From 25a07c30e5e19f13c32c7ab561391aefa38bc33d Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 20 May 2026 19:22:37 -0700 Subject: Don’t use VLA in etags.c mercury_decl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lib-src/etags.c (mercury_decl): Don’t use a VLA, as C11+ says VLAs are optional. Instead, redo to omit the need for an array at all. --- lib-src/etags.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'lib-src') diff --git a/lib-src/etags.c b/lib-src/etags.c index f218dba2902..4114ba6b655 100644 --- a/lib-src/etags.c +++ b/lib-src/etags.c @@ -6654,6 +6654,14 @@ static const char *Mercury_decl_tags[] = {"type", "solver type", "pred", "initialise", "finalise", "mutable", "module", "interface", "implementation", "import_module", "use_module", "include_module", "end_module", "some", "all"}; +/* Return true if array of char BUF, of length LEN, equals STR. */ + +static bool +memstreq (char const *buf, ptrdiff_t len, char const *str) +{ + return strlen (str) == len && memeq (buf, str, len); +} + static mercury_pos_t mercury_decl (char *s, size_t pos) { @@ -6661,27 +6669,24 @@ mercury_decl (char *s, size_t pos) if (s == NULL) return null_pos; - size_t origpos; - origpos = pos; + size_t origpos = pos; + char *decl_type = s + origpos; while (c_isalnum (s[pos]) || s[pos] == '_') pos++; - unsigned char decl_type_length = pos - origpos; - char buf[decl_type_length + 1]; - memset (buf, 0, decl_type_length + 1); + ptrdiff_t decl_type_length = pos - origpos; /* Mercury declaration tags. Consume them, then check the declaration item following :- is legitimate, then go on as in the prolog case. */ - memcpy (buf, &s[origpos], decl_type_length); - bool found_decl_tag = false; if (is_mercury_quantifier) { - if (strcmp (buf, "pred") != 0 && strcmp (buf, "func") != 0) /* Bad syntax. */ - return null_pos; + if (! (memstreq (decl_type, decl_type_length, "pred") + || memstreq (decl_type, decl_type_length, "func"))) + return null_pos; /* Bad syntax. */ is_mercury_quantifier = false; /* Reset to base value. */ found_decl_tag = true; @@ -6690,14 +6695,14 @@ mercury_decl (char *s, size_t pos) { for (int j = 0; j < sizeof (Mercury_decl_tags) / sizeof (char*); ++j) { - if (strcmp (buf, Mercury_decl_tags[j]) == 0) + if (memstreq (decl_type, decl_type_length, Mercury_decl_tags[j])) { found_decl_tag = true; - if (strcmp (buf, "type") == 0) + if (memstreq (decl_type, decl_type_length, "type")) is_mercury_type = true; - if (strcmp (buf, "some") == 0 - || strcmp (buf, "all") == 0) + if (memstreq (decl_type, decl_type_length, "some") + || memstreq (decl_type, decl_type_length, "all")) { is_mercury_quantifier = true; } @@ -6707,18 +6712,15 @@ mercury_decl (char *s, size_t pos) else /* 'solver type' has a blank in the middle, so this is the hard case. */ - if (strcmp (buf, "solver") == 0) + if (memstreq (decl_type, decl_type_length, "solver")) { do pos++; while (c_isalnum (s[pos]) || s[pos] == '_'); decl_type_length = pos - origpos; - char buf2[decl_type_length + 1]; - memset (buf2, 0, decl_type_length + 1); - memcpy (buf2, &s[origpos], decl_type_length); - if (strcmp (buf2, "solver type") == 0) + if (memstreq (decl_type, decl_type_length, "solver type")) { found_decl_tag = false; break; /* Found declaration tag of rank j. */ -- cgit v1.3