Skip to content

Commit 63a8d7d

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/src/XCCDF/result.c: In function ‘xccdf_rule_result_set_time_current’: /home/jcerny/openscap/src/XCCDF/result.c:1578:46: warning: ‘%02d’ directive output may be truncated writing between 2 and 11 bytes into a region of size between 8 and 15 [-Wformat-truncation=] 1578 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", | ^~~~ /home/jcerny/openscap/src/XCCDF/result.c:1578:41: note: directive argument in the range [-2147483647, 2147483647] 1578 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/jcerny/openscap/src/XCCDF/result.c:1578:2: note: ‘snprintf’ output between 20 and 72 bytes into a destination of size 20 1578 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1579 | 1900 + lt->tm_year, 1 + lt->tm_mon, lt->tm_mday, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1580 | lt->tm_hour, lt->tm_min, lt->tm_sec); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/jcerny/openscap/src/XCCDF/result.c: In function ‘xccdf_result_set_start_time_current’: /home/jcerny/openscap/src/XCCDF/result.c:1578:46: warning: ‘%02d’ directive output may be truncated writing between 2 and 11 bytes into a region of size between 8 and 15 [-Wformat-truncation=] 1578 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", | ^~~~ /home/jcerny/openscap/src/XCCDF/result.c:1578:41: note: directive argument in the range [-2147483647, 2147483647] 1578 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/jcerny/openscap/src/XCCDF/result.c:1578:2: note: ‘snprintf’ output between 20 and 72 bytes into a destination of size 20 1578 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1579 | 1900 + lt->tm_year, 1 + lt->tm_mon, lt->tm_mday, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1580 | lt->tm_hour, lt->tm_min, lt->tm_sec); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/jcerny/openscap/src/XCCDF/result.c: In function ‘xccdf_result_set_end_time_current’: /home/jcerny/openscap/src/XCCDF/result.c:1578:46: warning: ‘%02d’ directive output may be truncated writing between 2 and 11 bytes into a region of size between 8 and 15 [-Wformat-truncation=] 1578 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", | ^~~~ /home/jcerny/openscap/src/XCCDF/result.c:1578:41: note: directive argument in the range [-2147483647, 2147483647] 1578 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/jcerny/openscap/src/XCCDF/result.c:1578:2: note: ‘snprintf’ output between 20 and 72 bytes into a destination of size 20 1578 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1579 | 1900 + lt->tm_year, 1 + lt->tm_mon, lt->tm_mday, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1580 | lt->tm_hour, lt->tm_min, lt->tm_sec); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 parent 73867d4 commit 63a8d7d

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/XCCDF/result.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,9 +1575,12 @@ static inline const char *_get_timestamp(void)
15751575

15761576
tm = time(NULL);
15771577
lt = localtime(&tm);
1578-
snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d",
1578+
int ret = snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d",
15791579
1900 + lt->tm_year, 1 + lt->tm_mon, lt->tm_mday,
15801580
lt->tm_hour, lt->tm_min, lt->tm_sec);
1581+
if (ret < 0) {
1582+
return NULL;
1583+
}
15811584
return timestamp;
15821585
}
15831586

0 commit comments

Comments
 (0)