Skip to content

Commit

Permalink
Correct function type of snprintf
Browse files Browse the repository at this point in the history
man snprintf:

Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).

The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')).  If the output  was
truncated  due  to this limit, then the return value is the number of characters (excluding the terminating null byte) which would have been
written to the final string if enough space had been available.  Thus, a return value of size or more means that the output  was  truncated.
(See also below under NOTES.)

If an output error is encountered, a negative value is returned.

Signed-off-by: Mikko Koivunalho <mikko.koivunalho@iki.fi>
  • Loading branch information
mikkoi committed Mar 8, 2020
1 parent 8a0c714 commit 8365a69
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions tests/check_check_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ void record_failure_line_num(int linenum)
{
size_t to_write;
size_t written;
int result;
int result, chars_printed;
char string[16];

/*
Expand All @@ -943,12 +943,13 @@ void record_failure_line_num(int linenum)
*/
linenum += 1;

to_write = snprintf(string, sizeof(string), "%d\n", linenum);
if(to_write == 0)
chars_printed = snprintf(string, sizeof(string), "%d\n", linenum);
if(chars_printed <= 0 || (size_t) chars_printed >= sizeof(string))
{
fprintf(stderr, "%s:%d: Error in call to snprintf:", __FILE__, __LINE__);
exit(1);
}
to_write = (size_t) chars_printed;

if(line_num_failures == NULL)
{
Expand Down

0 comments on commit 8365a69

Please sign in to comment.