From 0d97b5826534055fd224c61178b483c9c37f2c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Wed, 3 Jul 2019 15:37:17 +0200 Subject: [PATCH] Avoid potentially implementation-defined behavior when using a pipe as input --- src/asm/lexer.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/asm/lexer.c b/src/asm/lexer.c index dbc2c40e..ab22fa77 100644 --- a/src/asm/lexer.c +++ b/src/asm/lexer.c @@ -156,13 +156,32 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f) if (pBuffer == NULL) fatalerror("%s: Out of memory!", __func__); - size_t size = 0, capacity; + size_t size = 0, capacity = -1; char *buf = NULL; - fseek(f, 0, SEEK_END); - capacity = ftell(f); - rewind(f); + /* + * Check if we can get the file size without implementation-defined + * behavior: + * + * From ftell(3p): + * [On error], ftell() and ftello() shall return −1, and set errno to + * indicate the error. + * + * The ftell() and ftello() functions shall fail if: [...] + * ESPIPE The file descriptor underlying stream is associated with a + * pipe, FIFO, or socket. + * + * From fseek(3p): + * The behavior of fseek() on devices which are incapable of seeking + * is implementation-defined. + */ + if (ftell(f) != -1) { + fseek(f, 0, SEEK_END); + capacity = ftell(f); + rewind(f); + } + // If ftell errored or the block above wasn't executed if (capacity == -1) capacity = 4096;