Skip to content

Commit 872f41c

Browse files
committed
Ensure snprintf output will never be truncated
Addressing: /home/jcerny/openscap/src/XCCDF/item.c: In function ‘xccdf_status_to_dom’: /home/jcerny/openscap/src/XCCDF/item.c:547:45: warning: ‘-’ directive output may be truncated writing 1 byte into a region of size between 0 and 7 [-Wformat-truncation=] 547 | snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); | ^ /home/jcerny/openscap/src/XCCDF/item.c:547:40: note: directive argument in the range [-2147483647, 2147483647] 547 | snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); | ^~~~~~~~~~~~~~~~ /home/jcerny/openscap/src/XCCDF/item.c:547:3: note: ‘snprintf’ output between 11 and 36 bytes into a destination of size 11 547 | snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 parent cbf89aa commit 872f41c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/XCCDF/item.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,16 @@ xmlNode *xccdf_status_to_dom(struct xccdf_status *status, xmlDoc *doc, xmlNode *
543543
time_t date_time = xccdf_status_get_date(status);
544544
if (date_time) {
545545
struct tm *date = localtime(&date_time);
546-
char date_str[] = "YYYY-DD-MM";
547-
snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
546+
/* "YYYY-DD-MM" */
547+
int date_len = snprintf(NULL, 0, "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
548+
if (date_len < 0) {
549+
return status_node;
550+
}
551+
date_len++; // +1 for terminating '\0'
552+
char *date_str = malloc(date_len);
553+
snprintf(date_str, date_len, "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
548554
xmlNewProp(status_node, BAD_CAST "date", BAD_CAST date_str);
555+
free(date_str);
549556
}
550557

551558
return status_node;

0 commit comments

Comments
 (0)