From fee89ee4a91f0d1eed465ca785b5353f02913ab4 Mon Sep 17 00:00:00 2001 From: Ryan Kelly Date: Tue, 25 Feb 2014 17:15:58 +1100 Subject: Ensure that fileno() returns -1 when given an invalid file pointer. --- src/library.js | 4 +++- tests/test_core.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/library.js b/src/library.js index 5df5ff20..b9571a73 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 19bb9ca1..c40d73f4 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -4058,6 +4058,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 + #include + 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) -- cgit v1.2.3-18-g5258