From 1d5b34b52a77767763168175972aef3ebabbdf54 Mon Sep 17 00:00:00 2001 From: rlar Date: Sun, 28 Feb 2016 16:49:53 +0100 Subject: change return type and rename int htoi()/otoi() --> unsigned int htoui()/otoui() --- src/misc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index a2f67fc..62a29d9 100644 --- a/src/misc.c +++ b/src/misc.c @@ -326,9 +326,9 @@ void flexfatal (const char *msg) } -/* htoi - convert a hexadecimal digit string to an integer value */ +/* htoui - convert a hexadecimal digit string to an unsigned integer value */ -int htoi (unsigned char str[]) +unsigned int htoui (unsigned char str[]) { unsigned int result; @@ -547,7 +547,7 @@ unsigned char myesc (unsigned char array[]) c = array[sptr]; array[sptr] = '\0'; - esc_char = otoi (array + 1); + esc_char = (unsigned char) otoui (array + 1); array[sptr] = c; @@ -569,7 +569,7 @@ unsigned char myesc (unsigned char array[]) c = array[sptr]; array[sptr] = '\0'; - esc_char = htoi (array + 2); + esc_char = (unsigned char) htoui (array + 2); array[sptr] = c; @@ -582,9 +582,9 @@ unsigned char myesc (unsigned char array[]) } -/* otoi - convert an octal digit string to an integer value */ +/* otoui - convert an octal digit string to an unsigned integer value */ -int otoi (unsigned char str[]) +unsigned int otoui (unsigned char str[]) { unsigned int result; -- cgit v1.2.3 From 9160ceb67ff5317753ff71c623b037126862a32f Mon Sep 17 00:00:00 2001 From: rlar Date: Sun, 28 Feb 2016 21:12:45 +0100 Subject: cast to get rid of warnings --- src/misc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index 62a29d9..ceda60d 100644 --- a/src/misc.c +++ b/src/misc.c @@ -167,7 +167,7 @@ void add_action (const char *new_text) void *allocate_array (int size, size_t element_size) { void *mem; - size_t num_bytes = element_size * size; + size_t num_bytes = element_size * (size_t) size; mem = malloc(num_bytes); if (!mem) @@ -692,7 +692,7 @@ char *readable_form (int c) return "' '"; else { - rform[0] = c; + rform[0] = (char) c; rform[1] = '\0'; return rform; @@ -705,7 +705,7 @@ char *readable_form (int c) void *reallocate_array (void *array, int size, size_t element_size) { void *new_array; - size_t num_bytes = element_size * size; + size_t num_bytes = element_size * (size_t) size; new_array = realloc(array, num_bytes); if (!new_array) -- cgit v1.2.3 From 925549475edb3c8921227fdd96aa0d90a2c0745a Mon Sep 17 00:00:00 2001 From: Demetri Obenour Date: Mon, 23 May 2016 17:36:33 -0400 Subject: Delete action_m4_define from misc.c This function was not used and always triggered a fatal error when run. --- src/misc.c | 24 ------------------------ 1 file changed, 24 deletions(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index ceda60d..753ada3 100644 --- a/src/misc.c +++ b/src/misc.c @@ -111,30 +111,6 @@ void action_define (const char *defname, int value) buf_append (&defs_buf, &cpy, 1); } - -#ifdef notdef -/** Append "m4_define([[defname]],[[value]])m4_dnl\n" to the running buffer. - * @param defname The macro name. - * @param value The macro value, can be NULL, which is the same as the empty string. - */ -static void action_m4_define (const char *defname, const char * value) -{ - char buf[MAXLINE]; - - flexfatal ("DO NOT USE THIS FUNCTION!"); - - if ((int) strlen (defname) > MAXLINE / 2) { - format_pinpoint_message (_ - ("name \"%s\" ridiculously long"), - defname); - return; - } - - snprintf (buf, sizeof(buf), "m4_define([[%s]],[[%s]])m4_dnl\n", defname, value?value:""); - add_action (buf); -} -#endif - /* Append "new_text" to the running buffer. */ void add_action (const char *new_text) { -- cgit v1.2.3 From 9d3ddf572e3744e4cf5e9788b676f423fe69aee8 Mon Sep 17 00:00:00 2001 From: Demi Obenour Date: Tue, 27 Sep 2016 10:54:16 -0400 Subject: Fix M4 quoting of section 3. This fixes M4 quoting of section 3 of the input file, including escape sequences and character constants. Tests were added to verify the behavior in section 3 with respect to quoting. Both escaping of quotes and quoting of potential macro-start characters are tested. Existing tests were also fixed to account for the new -- and now correct -- behavior. Many tests relied on the old behavior of expanding M4 macros in section 3. They needed to be updated for the new behavior. --- src/misc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index 753ada3..e4e3f88 100644 --- a/src/misc.c +++ b/src/misc.c @@ -362,8 +362,8 @@ void line_directive_out (FILE *output_file, int do_infile) s3 = &filename[sizeof (filename) - 2]; while (s2 < s3 && *s1) { - if (*s1 == '\\') - /* Escape the '\' */ + if (*s1 == '\\' || *s1 == '"') + /* Escape the '\' or '"' */ *s2++ = '\\'; *s2++ = *s1++; @@ -512,7 +512,8 @@ unsigned char myesc (unsigned char array[]) { /* \ */ int sptr = 1; - while (isascii (array[sptr]) && + while (sptr <= 3 && + isascii (array[sptr]) && isdigit (array[sptr])) /* Don't increment inside loop control * because if isdigit() is a macro it might -- cgit v1.2.3 From 6f47a5232cc26a53ccdf4d49d5b678f1a6fc4999 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Sun, 22 Jan 2017 18:23:40 +0100 Subject: scanner: Include stdarg.h for va_list --- src/misc.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index e4e3f88..e7f58b5 100644 --- a/src/misc.c +++ b/src/misc.c @@ -30,7 +30,6 @@ /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ /* PURPOSE. */ - #include "flexdef.h" #include "tables.h" -- cgit v1.2.3 From 1a9468797d6ed418beb313a0e3ff18b8e886dde5 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Sun, 22 Jan 2017 18:30:51 +0100 Subject: scanner: Use array instead of pointer --- src/misc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index e7f58b5..6eee161 100644 --- a/src/misc.c +++ b/src/misc.c @@ -347,7 +347,7 @@ void line_directive_out (FILE *output_file, int do_infile) { char directive[MAXLINE], filename[MAXLINE]; char *s1, *s2, *s3; - static const char *line_fmt = "#line %d \"%s\"\n"; + static const char line_fmt[] = "#line %d \"%s\"\n"; if (!gen_line_dirs) return; -- cgit v1.2.3 From 9c54eb6e30459e74a4de37822b497b0b3dc73995 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Thu, 19 Jan 2017 16:04:13 +0800 Subject: build: detect overflow for [re]allocate_array. Use reallocarray() when we have it (i.e. in OpenBSD system). When we don't, use equivalent overflow detection for our allocate_array and reallocate_array functions. Remove lib/reallocarray.c from our LIBOBJS as we no longer need it. Provide a fallback SIZE_MAX macro definition in flexint.h (not preprocessor friendly, but enough for our reallocate_array use case). --- src/misc.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index 6eee161..e17b1a2 100644 --- a/src/misc.c +++ b/src/misc.c @@ -142,9 +142,14 @@ void add_action (const char *new_text) void *allocate_array (int size, size_t element_size) { void *mem; - size_t num_bytes = element_size * (size_t) size; - - mem = malloc(num_bytes); +#if HAVE_REALLOCARRAY + /* reallocarray has built-in overflow detection */ + mem = reallocarray(NULL, (size_t) size, element_size); +#else + size_t num_bytes = (size_t) size * element_size; + mem = (size && SIZE_MAX / (size_t) size < element_size) ? NULL : + malloc(num_bytes); +#endif if (!mem) flexfatal (_ ("memory allocation failed in allocate_array()")); @@ -681,9 +686,14 @@ char *readable_form (int c) void *reallocate_array (void *array, int size, size_t element_size) { void *new_array; - size_t num_bytes = element_size * (size_t) size; - - new_array = realloc(array, num_bytes); +#if HAVE_REALLOCARRAY + /* reallocarray has built-in overflow detection */ + new_array = reallocarray(array, (size_t) size, element_size); +#else + size_t num_bytes = (size_t) size * element_size; + new_array = (size && SIZE_MAX / (size_t) size < element_size) ? NULL : + realloc(array, num_bytes); +#endif if (!new_array) flexfatal (_("attempt to increase array size failed")); -- cgit v1.2.3 From d4ab90f185e18328b834cb80886b5be15f1019fe Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Tue, 11 Apr 2017 02:10:29 +0800 Subject: Obsolete htoui() and otoui(); use strtoul(). No sense to keep these two function when libc's strtoul() can do the same job, but better. --- src/misc.c | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index e17b1a2..d5d6b89 100644 --- a/src/misc.c +++ b/src/misc.c @@ -306,18 +306,6 @@ void flexfatal (const char *msg) } -/* htoui - convert a hexadecimal digit string to an unsigned integer value */ - -unsigned int htoui (unsigned char str[]) -{ - unsigned int result; - - (void) sscanf ((char *) str, "%x", &result); - - return result; -} - - /* lerr - report an error message */ void lerr (const char *msg, ...) @@ -528,7 +516,7 @@ unsigned char myesc (unsigned char array[]) c = array[sptr]; array[sptr] = '\0'; - esc_char = (unsigned char) otoui (array + 1); + esc_char = (unsigned char) strtoul (array + 1, NULL, 8); array[sptr] = c; @@ -550,7 +538,7 @@ unsigned char myesc (unsigned char array[]) c = array[sptr]; array[sptr] = '\0'; - esc_char = (unsigned char) htoui (array + 2); + esc_char = (unsigned char) strtoul (array + 2, NULL, 16); array[sptr] = c; @@ -563,17 +551,6 @@ unsigned char myesc (unsigned char array[]) } -/* otoui - convert an octal digit string to an unsigned integer value */ - -unsigned int otoui (unsigned char str[]) -{ - unsigned int result; - - (void) sscanf ((char *) str, "%o", &result); - return result; -} - - /* out - various flavors of outputing a (possibly formatted) string for the * generated scanner, keeping track of the line count. */ -- cgit v1.2.3 From 47d6a453457f0c33f9fd0c4e45ada1d5bb1e20ca Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Tue, 11 Apr 2017 02:16:22 +0800 Subject: Fix myesc() 'sptr' conditionals * Don't call isascii() here. It's deprecated in POSIX and not needed for myesc's case. * The check of the character class and range here should match what's defined as {ESCSEQ} in scan.l, so for [[:xdigit:]] we use isxdigit(); for [0-7] we check '0' <= c <= '7' (not isdigit(c) because isdigit is locale-dependant in standard's sense) * Add missing length limit for "\x" ( is at most 2 digits) --- src/misc.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index d5d6b89..cb7dba4 100644 --- a/src/misc.c +++ b/src/misc.c @@ -505,13 +505,9 @@ unsigned char myesc (unsigned char array[]) int sptr = 1; while (sptr <= 3 && - isascii (array[sptr]) && - isdigit (array[sptr])) - /* Don't increment inside loop control - * because if isdigit() is a macro it might - * expand into multiple increments ... - */ + array[sptr] >= '0' && array[sptr] <= '7') { ++sptr; + } c = array[sptr]; array[sptr] = '\0'; @@ -527,13 +523,13 @@ unsigned char myesc (unsigned char array[]) { /* \x */ int sptr = 2; - while (isascii (array[sptr]) && - isxdigit (array[sptr])) + while (sptr <= 3 && isxdigit (array[sptr])) { /* Don't increment inside loop control - * because if isdigit() is a macro it might + * because if isxdigit() is a macro it might * expand into multiple increments ... */ ++sptr; + } c = array[sptr]; array[sptr] = '\0'; -- cgit v1.2.3 From 2f21edac99b5efc432417233e6e53326d630e08f Mon Sep 17 00:00:00 2001 From: Demi Obenour Date: Wed, 3 May 2017 10:29:01 -0400 Subject: build: Delete comments from skeleton file. Since the comments in flex.skl are, by hypothesis, not needed in skel.c, we remove them. THis reduces the size of the resulting executable somewhat. --- src/misc.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index cb7dba4..ef27833 100644 --- a/src/misc.c +++ b/src/misc.c @@ -791,9 +791,6 @@ void skelout (void) /* %e end linkage-only code. */ OUT_END_CODE (); } - else if (buf[1] == '#') { - /* %# a comment in the skel. ignore. */ - } else { flexfatal (_("bad line in skeleton file")); } -- cgit v1.2.3