DrKLO/Telegram · error

Invalid payload length\n

Error message

Invalid payload length\n

What it means

The tool reads a 4-byte big-endian length prefix via char_to_int() for each packet. If the decoded length exceeds 1500 bytes or is negative, and the stream is not at EOF, the input is considered corrupt. This guard exists because the packets buffer is sized [1500] per packet (line 62); a length beyond 1500 would overflow that buffer. A clean EOF is handled separately (setting eof=1 and breaking the loop).

Source

Thrown at TMessagesProj/jni/opus/src/repacketizer_demo.c:131

   rp = opus_repacketizer_create();
   while (!eof)
   {
      int err;
      int nb_packets=merge;
      opus_repacketizer_init(rp);
      for (i=0;i<nb_packets;i++)
      {
         unsigned char ch[4];
         err = fread(ch, 1, 4, fin);
         len[i] = char_to_int(ch);
         /*fprintf(stderr, "in len = %d\n", len[i]);*/
         if (len[i]>1500 || len[i]<0)
         {
             if (feof(fin))
             {
                eof = 1;
             } else {
                fprintf(stderr, "Invalid payload length\n");
                fclose(fin);
                fclose(fout);
                return EXIT_FAILURE;
             }
             break;
         }
         err = fread(ch, 1, 4, fin);
         rng[i] = char_to_int(ch);
         err = fread(packets[i], 1, len[i], fin);
         if (feof(fin))
         {
            eof = 1;
            break;
         }
         err = opus_repacketizer_cat(rp, packets[i], len[i]);
         if (err!=OPUS_OK)
         {
            fprintf(stderr, "opus_repacketizer_cat() failed: %s\n", opus_strerror(err));

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Ensure the input file was produced by opus_demo or a compatible tool that writes 4-byte length + 4-byte range-state + payload per packet.
  2. Validate the file format: the first 4 bytes should decode to a plausible packet length (1-1500).
  3. Regenerate the input file from the original encoder.
  4. If the file is intentionally in a different format, write a converter that emits the expected framing.
Defensive patterns

Strategy: validation

Validate before calling

// Validate input file framing before processing
int validate_packet_framing(FILE *fin) {
    unsigned char ch[4];
    long pos = ftell(fin);
    if (fread(ch, 1, 4, fin) != 4) { fseek(fin, pos, SEEK_SET); return -1; }
    int len = (ch[0]<<24)|(ch[1]<<16)|(ch[2]<<8)|ch[3];
    fseek(fin, pos, SEEK_SET);
    if (len < 0 || len > 1500) return -1;
    return 0;
}

Prevention

When it happens

Trigger: The input file is not in the expected length-prefixed format (e.g., raw Opus bitstream without the 4-byte length + 4-byte range headers); the file is truncated mid-header causing char_to_int to decode garbage; the file was written by a tool with a different endianness or format; bit corruption in the file.

Common situations: Feeding an .opus or .ogg container file instead of the raw length-prefixed packet dump; using output from a different codec or an older format version; file corruption during transfer.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/31ba8be7c49e5758. Report an issue: GitHub.