Skip to contentSkip to conversation

cutils: squelch compiler warnings with custom paths

PATCH New Paolo Bonzini 1 participant 1 message Oct 5, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cutils: squelch compiler warnings with custom paths

Setting --bindir= to an absolute path that is shorter than the prefix causes GCC to complain about array accesses out of bounds. The code however is safe, so disable the warning and explain why we are doing so.

---
 util/cutils.c | 7 +++++++
 1 file changed, 7 insertions(+)
util/cutils.c +7 -0
index 486acbdd673..f8030fe9f70 100644
@@ -1012,8 +1012,15 @@ int qemu_pstrcmp0(const char **str1, const char **str2)
 static inline bool starts_with_prefix(const char *dir)
 {
     size_t prefix_len = strlen(CONFIG_PREFIX);
+    /*
+     * dir[prefix_len] is only accessed if the length of dir is
+     * >= prefix_len, so no out of bounds access is possible.
+     */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Warray-bounds="
     return !memcmp(dir, CONFIG_PREFIX, prefix_len) &&
         (!dir[prefix_len] || G_IS_DIR_SEPARATOR(dir[prefix_len]));
+#pragma GCC diagnostic pop
 }
 
 /* Return the next path component in dir, and store its length in *p_len.  */
Signature · 1 line
2.41.0