Skip to content

Commit cbf89aa

Browse files
committed
Use snprintf in an expected way
If we want to get the size of buffer needed for snprintf, we can call snprintf with NULL, we don't need any temporary array. Addressing: /home/jcerny/openscap/src/XCCDF/benchmark.c: In function ‘xccdf_benchmark_gen_id’: /home/jcerny/openscap/src/XCCDF/benchmark.c:689:23: warning: ‘%03d’ directive output truncated writing 3 bytes into a region of size 1 [-Wformat-truncation=] 689 | const char *fmt = "%s%03d"; | ^~~~ /home/jcerny/openscap/src/XCCDF/benchmark.c:691:15: note: ‘snprintf’ output 4 or more bytes into a destination of size 1 691 | int length = snprintf(foo, 1, fmt, prefix, 0); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 parent 0927c7e commit cbf89aa

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

src/XCCDF/benchmark.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,7 @@ char *xccdf_benchmark_gen_id(struct xccdf_benchmark *benchmark, xccdf_type_t typ
687687
assert(prefix != NULL);
688688

689689
const char *fmt = "%s%03d";
690-
char foo[2];
691-
int length = snprintf(foo, 1, fmt, prefix, 0);
690+
int length = snprintf(NULL, 0, fmt, prefix, 0);
692691
if (length < 0)
693692
return NULL;
694693
length++;

0 commit comments

Comments
 (0)