Skip to content

Commit adcb940

Browse files
committed
Check snprintf return value
When possibility of truncating is expected or we are sure it will never happen, we should check the return value. If the return value is checked and program is branched, the GCC warning is not issued. See https://bugzilla.redhat.com/show_bug.cgi?id=1431678#c3 Addressing: /home/jcerny/openscap/utils/oscap-info.c: In function ‘app_info’: /home/jcerny/openscap/utils/oscap-info.c:90:46: warning: ‘-’ directive output may be truncated writing 1 byte into a region of size between 0 and 7 [-Wformat-truncation=] 90 | snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); | ^ /home/jcerny/openscap/utils/oscap-info.c:90:41: note: directive argument in the range [-2147483647, 2147483647] 90 | snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); | ^~~~~~~~~~~~~~~~ /home/jcerny/openscap/utils/oscap-info.c:90:4: note: ‘snprintf’ output between 11 and 36 bytes into a destination of size 11 90 | snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In function ‘_print_xccdf_tailoring_header’, inlined from ‘_print_xccdf_tailoring’ at /home/jcerny/openscap/utils/oscap-info.c:276:2, inlined from ‘app_info_single_ds_profiles_only’ at /home/jcerny/openscap/utils/oscap-info.c:333:4, inlined from ‘app_info_single_ds’ at /home/jcerny/openscap/utils/oscap-info.c:510:18, inlined from ‘app_info_sds’ at /home/jcerny/openscap/utils/oscap-info.c:542:7, inlined from ‘app_info’ at /home/jcerny/openscap/utils/oscap-info.c:654:7: /home/jcerny/openscap/utils/oscap-info.c:270:2: warning: ‘%s’ directive argument is null [-Wformat-overflow=] 270 | printf("%sBenchmark Hint: %s\n", prefix, xccdf_tailoring_get_benchmark_ref(tailoring)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/jcerny/openscap/utils/oscap-info.c: In function ‘app_info’: /home/jcerny/openscap/utils/oscap-info.c:90:46: warning: ‘-’ directive output may be truncated writing 1 byte into a region of size between 0 and 7 [-Wformat-truncation=] 90 | snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); | ^ /home/jcerny/openscap/utils/oscap-info.c:90:41: note: directive argument in the range [-2147483647, 2147483647] 90 | snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); | ^~~~~~~~~~~~~~~~ /home/jcerny/openscap/utils/oscap-info.c:90:4: note: ‘snprintf’ output between 11 and 36 bytes into a destination of size 11 90 | snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 parent 63a8d7d commit adcb940

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

utils/oscap-info.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ static inline void _print_xccdf_status(struct xccdf_status *status, const char *
8787
if (date_time != 0) {
8888
struct tm *date = localtime(&date_time);
8989
char date_str[] = "YYYY-DD-MM";
90-
snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
90+
int ret = snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
91+
if (ret < 0) {
92+
return;
93+
}
9194
printf("%sGenerated: %s\n", prefix, date_str);
9295
}
9396
}

0 commit comments

Comments
 (0)