From: Akim Demaille Date: Thu, 23 Feb 2012 19:14:42 +0000 (+0100) Subject: avoid direct strncmp calls. X-Git-Tag: v2.7.90~492 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/4663cb4d0c9b0ad8d5809a8911b9581db4d11126?ds=sidebyside avoid direct strncmp calls. Before this change, bison would accept either .tab and _tab equivalently, whatever the current platform. Besides, it was not obeying everywhere to the possible definition of TAB_EXT to something else than .tab. For consistency, handle only TAB_EXT (".tab" on non DJGPP platforms). Support for "_tab" is neither documented, nor tested. * src/system.h (STRNCMP_LIT): New. From Jim Meyering. (STRPREFIX_LIT): New. * src/files.c, src/getargs.c: Use it. --- diff --git a/src/files.c b/src/files.c index ceb0489a..550e42ed 100644 --- a/src/files.c +++ b/src/files.c @@ -215,10 +215,9 @@ file_name_split (const char *file_name, if (*ext) { size_t baselen = *ext - *base; - size_t dottablen = 4; + size_t dottablen = sizeof (TAB_EXT) - 1; if (dottablen < baselen - && (strncmp (*ext - dottablen, ".tab", dottablen) == 0 - || strncmp (*ext - dottablen, "_tab", dottablen) == 0)) + && STRPREFIX_LIT (TAB_EXT, *ext - dottablen)) *tab = *ext - dottablen; } } diff --git a/src/getargs.c b/src/getargs.c index 5e04ab0f..06e6e972 100644 --- a/src/getargs.c +++ b/src/getargs.c @@ -102,7 +102,7 @@ flags_argmatch (const char *option, args = strtok (args, ","); while (args) { - int no = strncmp (args, "no-", 3) == 0 ? 3 : 0; + int no = STRPREFIX_LIT ("no-", args) ? 3 : 0; int value = XARGMATCH (option, args + no, keys, values); if (value == 0) { diff --git a/src/system.h b/src/system.h index 7aa2c9fa..f0a76ceb 100644 --- a/src/system.h +++ b/src/system.h @@ -42,6 +42,15 @@ #define STREQ(L, R) (strcmp(L, R) == 0) #define STRNEQ(L, R) (!STREQ(L, R)) +/* Just like strncmp, but the second argument must be a literal string + and you don't specify the length. */ +#define STRNCMP_LIT(S, Literal) \ + strncmp (S, "" Literal "", sizeof (Literal) - 1) + +/* Whether Literal is a prefix of S. */ +#define STRPREFIX_LIT(Literal, S) \ + (STRNCMP_LIT (S, Literal) == 0) + #if HAVE_SYS_TYPES_H # include #endif