Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/torchcodec/_core/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ torch::Tensor validateSamples(const torch::Tensor& samples) {
samples.dim() == 2,
"samples must have 2 dimensions, got ",
samples.dim());
TORCH_CHECK(
samples.numel() == 0 || samples.data_ptr() != nullptr,
"Samples tensor has null data pointer but non-zero numel. ",
"This indicates an invalid tensor state.");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @pipitochka . Can you please share more details about how the fuzzer managed to create a tensor with numel() > 0 and data_ptr() == nullptr ?

I wonder how it's even possible to end up with such tensors through normal torch operations.

Note for myself: right now if we pass an a tensor with numel == 0 we get a decent error message:

E RuntimeError: Desired number of channels (0) is not supported by the encoder. Supported number of channels are: 1, 2.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I done it by torch::load(input_data, dir)

Copy link
Author

@pipitochka pipitochka Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My function to fuzzing

    char name[] = "/tmp/torch-fuzz-XXXXXX";
    char *dir = mktemp(name);
    std::ofstream fp;
    fp.open(dir, std::ios::out | std::ios::binary);
    fp.write((char *)data, size);
    fp.close();

    if (size <= 0) {
        unlink(dir);
        return 0;
    }

    try {
        torch::Tensor input_data;
        torch::load(input_data, dir);

        int mode = (int)data[0] % 5;
        facebook::torchcodec::AudioStreamOptions aso;
        aso.bitRate = mode;
        aso.numChannels = 1;
        aso.sampleRate = 1;
        std::string fileName = "tmp";

        facebook::torchcodec::AudioEncoder encoder = facebook::torchcodec::AudioEncoder(
        input_data, mode, fileName, aso);

        auto result = encoder.encodeToTensor();

    } catch (const c10::Error &e) {

    } catch (const torch::jit::ErrorReport &e) {

    } catch (const std::runtime_error &e) {

    }

    unlink(dir);
    return 0;


// We enforce this, but if we get user reports we should investigate whether
// that's actually needed.
Expand Down