DrKLO/Telegram · warning
bytes per packet must be between 0 and %d\n
Error message
bytes per packet must be between 0 and %d\n
What it means
The bytes-per-packet value (argv[4], parsed with atoi) is outside [0, MAX_PACKET]. Opus custom encoder output packets must fit in the MAX_PACKET-byte scratch buffer; this guard prevents buffer overflow when encoding into the fixed-size data[] array.
Source
Thrown at TMessagesProj/jni/opus/celt/opus_custom_demo.c:83
" <bytes per packet> [<complexity> [packet loss rate]] "
"<input> <output>\n");
return 1;
}
rate = (opus_int32)atol(argv[1]);
channels = atoi(argv[2]);
frame_size = atoi(argv[3]);
mode = opus_custom_mode_create(rate, frame_size, NULL);
if (mode == NULL)
{
fprintf(stderr, "failed to create a mode\n");
return 1;
}
bytes_per_packet = atoi(argv[4]);
if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET)
{
fprintf (stderr, "bytes per packet must be between 0 and %d\n",
MAX_PACKET);
return 1;
}
inFile = argv[argc-2];
fin = fopen(inFile, "rb");
if (!fin)
{
fprintf (stderr, "Could not open input file %s\n", argv[argc-2]);
return 1;
}
outFile = argv[argc-1];
fout = fopen(outFile, "wb+");
if (!fout)
{
fprintf (stderr, "Could not open output file %s\n", argv[argc-1]);
fclose(fin);
return 1;View on GitHub (pinned to 45ab8f4308)
Solutions
- Supply a bytes-per-packet in [0, MAX_PACKET]; check MAX_PACKET in opus_defines.h for the build's exact cap.
- Confirm argv[4] is the 4th positional arg (rate channels frame_size BYTES input output).
- Guard atoi with explicit digit-validation to reject garbage input.
Example fix
// before ./test_opus_custom 48000 1 960 4096 input.sw output.sw // after ./test_opus_custom 48000 1 960 100 input.sw output.sw
Defensive patterns
Strategy: validation
Validate before calling
// Range-check bytes_per_packet against MAX_PACKET before encoding.
if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET) {
fprintf(stderr, "bytes per packet must be between 0 and %d\n", MAX_PACKET);
return 1;
} Prevention
- Check MAX_PACKET in opus_defines.h for the build's cap before choosing a value.
- Validate argv[4] is numeric before atoi to avoid silent 0.
- Keep argument order straight: bytes_per_packet is the 4th positional arg.
When it happens
Trigger: bytes_per_packet < 0 or bytes_per_packet > MAX_PACKET. MAX_PACKET is the size of the local unsigned char data[MAX_PACKET] buffer used for encoded output. A negative value comes from atoi parsing a '-' prefix; an oversized value would overflow data[].
Common situations: Typing a bytes-per-packet larger than MAX_PACKET (commonly 127 or 1024 depending on build); passing a non-numeric string that atoi silently turns into 0; misreading the parameter order and supplying the frame size or rate in this slot.
Related errors
- Sampling rate must be 8000, 12000, 16000, 24000, or 48000\n
- Sample counts do not match (%lu!=%lu).\n
- Usage: test_opus_custom <rate> <channels> <frame size> <byt
- failed to create a mode\n
- Could not open input file %s\n
AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14).
Data as JSON: /api/errors/510379a46be339a3.
Report an issue: GitHub.