Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nbt_parse()'s use of inflate() and reader.read() #19

Open
Moosfet opened this issue Mar 24, 2023 · 1 comment
Open

nbt_parse()'s use of inflate() and reader.read() #19

Moosfet opened this issue Mar 24, 2023 · 1 comment

Comments

@Moosfet
Copy link

Moosfet commented Mar 24, 2023

I managed to parse half of the region files on my server before it got stuck in a loop, with this code calling my reader function repeatedly despite the reader function always returning 0 since there was no more data. I was too lazy to read the zlib documentation in full, but I did find a sentence which suggested ret == Z_OK && stream.avail_in != 0 is the correct condition to determine if inflate() needs to be called again. So I changed the while (stream.avail_out == 0) to while (ret == Z_OK && stream.avail_in != 0) and, IDK if that's correct, but it at least was able to process all of the region files on my server after that change.

In nbt_parse():

    do {
      stream.avail_in = reader.read(reader.userdata, in_buffer, NBT_BUFFER_SIZE);
      stream.next_in = in_buffer;

      do {
        stream.avail_out = NBT_BUFFER_SIZE;
        stream.next_out = out_buffer;

        ret = inflate(&stream, Z_NO_FLUSH);

        size_t have = NBT_BUFFER_SIZE - stream.avail_out;
        buffer = (uint8_t*)NBT_REALLOC(buffer, buffer_size + have);
        NBT_MEMCPY(buffer + buffer_size, out_buffer, have);
        buffer_size += have;

      } while (stream.avail_out == 0);
      
    } while (ret != Z_STREAM_END);
@Moosfet
Copy link
Author

Moosfet commented Mar 24, 2023

I forgot to mention that what made me suspect the while() condition was that I put assert(stream.avail_in == 0); before the call to reader.read() and it triggered on the same region file that was giving me trouble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant