{"record":{"id":"510379a46be339a3","repo":"DrKLO/Telegram","slug":"bytes-per-packet-must-be-between-0-and-d-n","errorCode":null,"errorMessage":"bytes per packet must be between 0 and %d\\n","messagePattern":"bytes per packet must be between 0 and (.+?)\\\\n","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"TMessagesProj/jni/opus/celt/opus_custom_demo.c","lineNumber":83,"sourceCode":"               \" <bytes per packet> [<complexity> [packet loss rate]] \"\n               \"<input> <output>\\n\");\n      return 1;\n   }\n\n   rate = (opus_int32)atol(argv[1]);\n   channels = atoi(argv[2]);\n   frame_size = atoi(argv[3]);\n   mode = opus_custom_mode_create(rate, frame_size, NULL);\n   if (mode == NULL)\n   {\n      fprintf(stderr, \"failed to create a mode\\n\");\n      return 1;\n   }\n\n   bytes_per_packet = atoi(argv[4]);\n   if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET)\n   {\n      fprintf (stderr, \"bytes per packet must be between 0 and %d\\n\",\n                        MAX_PACKET);\n      return 1;\n   }\n\n   inFile = argv[argc-2];\n   fin = fopen(inFile, \"rb\");\n   if (!fin)\n   {\n      fprintf (stderr, \"Could not open input file %s\\n\", argv[argc-2]);\n      return 1;\n   }\n   outFile = argv[argc-1];\n   fout = fopen(outFile, \"wb+\");\n   if (!fout)\n   {\n      fprintf (stderr, \"Could not open output file %s\\n\", argv[argc-1]);\n      fclose(fin);\n      return 1;","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/opus/celt/opus_custom_demo.c#L65-L101","documentation":"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.","triggerScenarios":"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[].","commonSituations":"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.","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."],"exampleFix":"// before\n./test_opus_custom 48000 1 960 4096 input.sw output.sw\n\n// after\n./test_opus_custom 48000 1 960 100 input.sw output.sw","handlingStrategy":"validation","validationCode":"// Range-check bytes_per_packet against MAX_PACKET before encoding.\nif (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET) {\n    fprintf(stderr, \"bytes per packet must be between 0 and %d\\n\", MAX_PACKET);\n    return 1;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["opus","validation","range-check","packet-size","buffer-overflow","c"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}