kripken/emscripten — pull request #2250 by abarbu (2014-03-22 19:08:15+00:00)
Title: Fix reading 0 bytes after ungetting a character

Running getc, ungetc, followed by getc produces an incorrect answer.

Right now fread returns immediately if asked to read 0 bytes. This is likely because the node.js backend throws an error (which sets the error bit on the stream) if asked to read 0 bytes from a file.

This check in fread is insufficient because the number of bytes to be read can fall back to zero after dealing with ungetting characters. The example below shows this bug (place in /tmp and run).
The second printf should show the same character twice, but at the moment it will print garbage.

```c
#include
int main() {
#ifdef EMSCRIPTEN
#include
EM_ASM(FS.mount(NODEFS, { root: '/tmp' }, '/tmp'));
#endif
FILE *f = fopen("/tmp/zerotest.c", "r");
printf("f %p\n", f);
int c = getc(f);
ungetc(c, f);
int d = getc(f);
printf("'%c' '%c'\n", c, d);
printf("'%zd'\n", fread(&c, 0, 0, f));
printf("'%zd'\n", fread(&c, 1, 1, f));
printf("'%c'\n", c);
return 0;
}
```