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

NODERAWFS bug in fseek/fwrite #6612

Closed
JanWielemaker opened this issue May 31, 2018 · 3 comments
Closed

NODERAWFS bug in fseek/fwrite #6612

JanWielemaker opened this issue May 31, 2018 · 3 comments

Comments

@JanWielemaker
Copy link

JanWielemaker commented May 31, 2018

Trying to figure out why minizip which is embedded in SWI-Prolog produces invalid zip files, I found that fseek()/fwrite() do not function properly. Below is a copy from the discussion

#include <stdio.h>

int
main(int argc, char **argv)
{ FILE *f = fopen("test.data", "wb");
  char data[] = "0123456789abcdef";
  char data2[] = "0123456789ABCDEF";
  char zero[16] = {0};

  fwrite(data, 1, 16, f);
  fwrite(zero, 1, 16, f);
  fwrite(data, 1, 16, f);
  fseek(f, SEEK_SET, 16);
  fwrite(data2, 1, 16, f);

  fclose(f);

  return 0;
}

Run using

emcc -o t.bc t.c
emcc t.bc -s NODERAWFS=1 -o t.html
node t.js
od -c test.data 
0000000   0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f
0000020  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000040   0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f
0000060   0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F

The zero block should have been written with data2, but instead, data2 is at the end.

Versions (on Ubuntu 18.04):

  • node v8.9.1
  • emcc 1.38.4

P.s. The original code checks all return values. These are fine. I also inserted an ftell() call after the fseek() that confirms the file pointer moved, but it didn't.

@JanWielemaker
Copy link
Author

JanWielemaker commented Jun 1, 2018

Bypassing the FILE stream layer avoids the problem: this works fine:

Edit: I mixed up. This too fails the same way. This also explains why minizip doesn't work through SWI-Prolog's own I/O streams that ultimately do open/read/write/lseek.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int
main(int argc, char **argv)
{ int f = open("test.data", O_CREAT|O_TRUNC|O_WRONLY, 0666);
  char data[] = "0123456789abcdef";
  char data2[] = "0123456789ABCDEF";
  char zero[16] = {0};

  write(f, data, 16);
  write(f, zero, 16);
  write(f, data, 16);
  lseek(f, 16, SEEK_SET);
  write(f, data2, 16);

  close(f);

  return 0;
}

@rla
Copy link

rla commented Jun 1, 2018

Related issue: #6381

@kripken
Copy link
Member

kripken commented Jul 10, 2018

Fixed by #6797.

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

3 participants