From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server-vie001.gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,URIBL_ZEN_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=new2025; t=1755724489; bh=wJ0QE5snbOY5HcyKwP5AghVb7bG8G+nNvlZHWlXsOCs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Message-ID:Date:From: Reply-To:Subject:To:Cc:In-Reply-To:References:Resent-Date: Resent-From:Resent-To:Resent-Cc:User-Agent:Content-Type: Content-Transfer-Encoding; b=GARb7BG/+gA+1Ij9/PSEJ70H+8R6iL2EJD9GR19CkCVCc9WNlHfrz6sDI9Vwn46Xe eiKfFTgmNiTvrQPMggp8FMNKT3BL3Q5AhqZ4pCPlJYwx3s0ZSs9AkE8F/VJLUZ7G97 JBm5HPKQBHAmc2GD8jYDNdApX0qFfytw8jmNN0HuWQ1NEZOo8aDyjGnaij2CK6+3qi 2ludkKqRr3M6h7FMpy44fWef2HfbxxqYMHE6oAGyvoiU2azjx3lkksnfR94GCBLO9e DU5Sz9ba+D4mt75NCJIDglRq5SRJQoJVE1yVKcSu7X80GEVwaPzfQnZNA/yINhjIXA sgvUs44iYmXYg== Received: from integral2.. (unknown [103.216.223.55]) by server-vie001.gnuweeb.org (Postfix) with ESMTPSA id 29C0D3127F84; Wed, 20 Aug 2025 21:14:46 +0000 (UTC) From: Ammar Faizi To: Steven Rostedt Cc: Ammar Faizi , Linux Trace Devel Mailing List , GNU/Weeb Mailing List , Alviro Iskandar Setiawan Subject: [PATCH 2/2] ccli: commands: Fix optimized `strlen()` calls Date: Thu, 21 Aug 2025 04:14:34 +0700 Message-Id: <20250820211434.1857859-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250820211434.1857859-1-ammarfaizi2@gnuweeb.org> References: <20250820211434.1857859-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: When compiling with clang, the following warning appears: commands.c:291:6: warning: variable 'l' set but not used [-Wunused-but-set-variable] 291 | int l = 0; | ^ Closer inspection shows that `l` is only used to accumulate the return values of `strlen()` calls, but its value is never read. It turns out that it indicates a deeper issue than a simple unused variable. The purpose of the `strlen()` calls in this code is to ensure all strings passed to `test_table()` are touched. However, `strlen()` is declared with `__attribute_pure__`. A pure function always produces the same result for the same input and has no side effects. If the result of `strlen()` is not used, the compiler is allowed to remove the call entirely, which means the strings are never actually touched. For example, compiling with `-O3`: #include void touch_string(char *s) { strlen(s); } produces only: touch_string: retq Even at `-O0`, gcc still omits the `strlen()` call: touch_string: pushq %rbp movq %rsp,%rbp movq %rdi,-0x8(%rbp) nop popq %rbp retq Introduce a new macro, OPTIMIZER_HIDE_VAR(), to hide the variable from the compiler's optimizer, keeping the strlen() calls intact. It also cleans the clang warning. Co-authored-by: Alviro Iskandar Setiawan Signed-off-by: Alviro Iskandar Setiawan Signed-off-by: Ammar Faizi --- Giving more context for src/commands.c hunk for easier review. src/ccli-local.h | 1 + src/commands.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/ccli-local.h b/src/ccli-local.h index 31d16de73095..d7fd8e71e482 100644 --- a/src/ccli-local.h +++ b/src/ccli-local.h @@ -15,6 +15,7 @@ #define __hidden __attribute__((visibility ("hidden"))) #define ISSPACE(c) isspace((unsigned char)(c)) +#define OPTIMIZER_HIDE_VAR(X) __asm__ volatile ("": "+r" (X)) struct line_buf { char *line; diff --git a/src/commands.c b/src/commands.c --- a/src/commands.c +++ b/src/commands.c @@ -285,49 +285,50 @@ int ccli_execute(struct ccli *ccli, const char *line_str, bool hist) static void test_table(const void *data) { const struct ccli_command_table *table = data; const char *name = table->name; int len = strlen(test_name); int l = 0; int i; /* The root of the table does not need a name */ if (!name) name = "root"; if (len) strncat(test_name, "->", BUFSIZ - 1); strncat(test_name, name, BUFSIZ - 1); snprintf(test_message, BUFSIZ - 1, "Command table missing name or 'subcommands' NULL terminator"); /* Touch all the names first */ for (i = 0; table->subcommands[i]; i++) l += strlen(table->subcommands[i]->name); snprintf(test_message, BUFSIZ - 1, "Command table missing 'options' NULL terminator"); if (table->options) { int clen; /* Test the options too */ clen = strlen(test_name); if (clen) strncat(test_name, "->[options]", BUFSIZ - 1); for (i = 0; table->options->options[i]; i++) l += strlen(table->options->options[i]->name); test_name[clen] = '\0'; } /* Now recurse */ for (i = 0; table->subcommands[i]; i++) test_table(table->subcommands[i]); test_name[len] = '\0'; + OPTIMIZER_HIDE_VAR(l); } -- Ammar Faizi