diff mbox series

xfer-opitons: Fix -Wformat=2 warnings

Message ID 20191222025458.745140-1-rosenp@gmail.com (mailing list archive)
State New, archived
Headers show
Series xfer-opitons: Fix -Wformat=2 warnings | expand

Commit Message

Rosen Penev Dec. 22, 2019, 2:54 a.m. UTC
Allows the compiler to check types.

format string functions expect constant expressions, not constant
variables.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 axfer/xfer-options.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

Comments

Takashi Sakamoto Dec. 24, 2019, 10:13 a.m. UTC | #1
On Sat, Dec 21, 2019 at 06:54:58PM -0800, Rosen Penev wrote:
> Allows the compiler to check types.
> 
> format string functions expect constant expressions, not constant
> variables.
> 
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>  axfer/xfer-options.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)

Reviewd-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>

According to reference manual of GCC[1], '-Wformat=2' is a easy helper to
enable below options:
 * -Wformat
 * -Wformat-nonliteral
 * -Wformat-security
 * -Wformat-y2k

Your fix suppresses the warning from '-Wformat-nonliteral'. I'd like
maintainers to revise the title of patch.

[1] https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html


Regards

Takashi Sakamoto
diff mbox series

Patch

diff --git a/axfer/xfer-options.c b/axfer/xfer-options.c
index 3740b16..d4e5ff2 100644
--- a/axfer/xfer-options.c
+++ b/axfer/xfer-options.c
@@ -422,8 +422,6 @@  static int generate_path_with_suffix(struct xfer_context *xfer,
 				     const char *template, unsigned int index,
 				     const char *suffix)
 {
-	static const char *const single_format = "%s%s";
-	static const char *const multiple_format = "%s-%i%s";
 	unsigned int len;
 
 	len = strlen(template) + strlen(suffix) + 1;
@@ -435,10 +433,10 @@  static int generate_path_with_suffix(struct xfer_context *xfer,
 		return -ENOMEM;
 
 	if (xfer->path_count == 1) {
-		snprintf(xfer->paths[index], len, single_format, template,
+		snprintf(xfer->paths[index], len, "%s%s", template,
 			 suffix);
 	} else {
-		snprintf(xfer->paths[index], len, multiple_format, template,
+		snprintf(xfer->paths[index], len, "%s-%i%s", template,
 			 index, suffix);
 	}
 
@@ -449,8 +447,6 @@  static int generate_path_without_suffix(struct xfer_context *xfer,
 				        const char *template,
 					unsigned int index, const char *suffix)
 {
-	static const char *const single_format = "%s";
-	static const char *const multiple_format = "%s-%i";
 	unsigned int len;
 
 	len = strlen(template) + 1;
@@ -462,9 +458,9 @@  static int generate_path_without_suffix(struct xfer_context *xfer,
 		return -ENOMEM;
 
 	if (xfer->path_count == 1) {
-		snprintf(xfer->paths[index], len, single_format, template);
+		snprintf(xfer->paths[index], len, "%s", template);
 	} else {
-		snprintf(xfer->paths[index], len, multiple_format, template,
+		snprintf(xfer->paths[index], len, "%s-%i", template,
 			index);
 	}