Hi Alexander,
thank you for your patch.
I think your patched version of strspn() assumes that all of these are true:
- s1 is NULL-terminated or has characters not in s2;
- s2 doesn't contain '\n';
- s1 is not an empty string (pointing to NULL).
Also it redefines a libc function with a macro which doesn't seem very clean.
The attached version is safer in that it is bounded and avoids shadowing a
stdlib function.
Regards,
Fedor Efimtsev
From 2830f066ca295d3731639a9f577cf5f4c7cf8076 Mon Sep 17 00:00:00 2001
From: Fedor Efimtsev <[email protected]>
Date: Mon, 7 Sep 2026 13:40:56 +0700
Subject: [PATCH] fix sanitizer warning in src/tools/pg_bsd_indent/lexi.c
---
src/tools/pg_bsd_indent/lexi.c | 34 +++++++++++++++++++++++++++++++---
1 file changed, 31 insertions(+), 3 deletions(-)
diff --git a/src/tools/pg_bsd_indent/lexi.c b/src/tools/pg_bsd_indent/lexi.c
index e846188d6f4..7f28e631f43 100644
--- a/src/tools/pg_bsd_indent/lexi.c
+++ b/src/tools/pg_bsd_indent/lexi.c
@@ -212,6 +212,34 @@ is_func_definition(char *tp)
return false;
}
+static size_t
+indent_strspn(const char *s1, const char *s2, const char *end)
+{
+ const char *p = s1;
+
+ while (p < end && *p != '\n' && *p != '\0')
+ {
+ const char *spanp;
+ bool found = false;
+
+ for (spanp = s2; *spanp != '\0'; spanp++)
+ {
+ if ((unsigned char) *spanp == (unsigned char) *p)
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ break;
+
+ p++;
+ }
+
+ return p - s1;
+}
+
int
lexi(struct parser_state *state)
{
@@ -255,11 +283,11 @@ lexi(struct parser_state *state)
int len;
if (buf_ptr[1] == 'b' || buf_ptr[1] == 'B')
- len = strspn(buf_ptr + 2, "01") + 2;
+ len = indent_strspn(buf_ptr + 2, "01", buf_end) + 2;
else if (buf_ptr[1] == 'x' || buf_ptr[1] == 'X')
- len = strspn(buf_ptr + 2, "0123456789ABCDEFabcdef") + 2;
+ len = indent_strspn(buf_ptr + 2, "0123456789ABCDEFabcdef", buf_end) + 2;
else
- len = strspn(buf_ptr + 1, "012345678") + 1;
+ len = indent_strspn(buf_ptr + 1, "012345678", buf_end) + 1;
if (len > 0) {
CHECK_SIZE_TOKEN(len);
memcpy(e_token, buf_ptr, len);
--
2.55.0