aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-02-26 10:46:32 -0800
committerAlon Zakai <alonzakai@gmail.com>2014-02-26 10:46:32 -0800
commit4c015a33bc5ed8b4e7f9ef7c80e467cce8af5ad6 (patch)
treeb090998f9135ad06383eb2a2a5b312cabbeb5a68
parent01efb0bfc550ed1e4ebd4cfc2c617dea83026029 (diff)
parentfee89ee4a91f0d1eed465ca785b5353f02913ab4 (diff)
Merge pull request #2157 from rfk/rfk/fileno-error-reporting
Ensure that fileno() returns -1 when given an invalid file pointer.
-rw-r--r--src/library.js4
-rw-r--r--tests/test_core.py22
2 files changed, 25 insertions, 1 deletions
diff --git a/src/library.js b/src/library.js
index 18a794fd..cd451c57 100644
--- a/src/library.js
+++ b/src/library.js
@@ -2420,7 +2420,9 @@ LibraryManager.library = {
fileno: function(stream) {
// int fileno(FILE *stream);
// http://pubs.opengroup.org/onlinepubs/000095399/functions/fileno.html
- return FS.getStreamFromPtr(stream).fd;
+ stream = FS.getStreamFromPtr(stream);
+ if (!stream) return -1;
+ return stream.fd;
},
ftrylockfile: function() {
// int ftrylockfile(FILE *file);
diff --git a/tests/test_core.py b/tests/test_core.py
index 015f65bf..f67d820a 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -4066,6 +4066,28 @@ def process(filename):
self.emcc_args += ['--embed-file', 'three_numbers.txt']
self.do_run(src, 'match = 3\nx = -1.0, y = 0.1, z = -0.1\n')
+ def test_fileno(self):
+ if self.emcc_args is None: return self.skip('requires emcc')
+ open(os.path.join(self.get_dir(), 'empty.txt'), 'w').write('')
+ src = r'''
+ #include <stdio.h>
+ #include <unistd.h>
+ int main()
+ {
+ FILE* fp = fopen("empty.txt", "r");
+ if (fp) {
+ printf("%d\n", fp);
+ printf("%d\n", fileno(fp));
+ printf("%d\n", fileno((FILE*)42)); // nonexistent stream
+ } else {
+ printf("failed to open empty.txt\n");
+ }
+ return 0;
+ }
+ '''
+ self.emcc_args += ['--embed-file', 'empty.txt']
+ self.do_run(src, '4\n3\n-1\n')
+
def test_readdir(self):
src = open(path_from_root('tests', 'dirent', 'test_readdir.c'), 'r').read()
self.do_run(src, 'success', force_c=True)