Skip to content

Commit

Permalink
tests: sprintf: suppress Wformat-truncation warning
Browse files Browse the repository at this point in the history
GCC 7 and newer are smart enough to realize that in test_snprintf()
the output will not fit in 0 or 4 bytes, but that it requires 9.
So it throws a warning in compile time. But in this case we are
actually testing that snprintf's return value is what it should be
while truncating the output. So let's suppress this warning here.

Fixes: zephyrproject-rtos#5732

Signed-off-by: Alberto Escolar Piedras <[email protected]>
  • Loading branch information
aescolar authored and carlescufi committed Jun 28, 2018
1 parent e860775 commit 2b02f8d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tests/lib/sprintf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,22 @@ void test_vsprintf(void)

void test_snprintf(void)
{
#if defined(__GNUC__) && __GNUC__ >= 7
/*
* GCC 7 and newer are smart enough to realize that in the statements
* below, the output will not fit in 0 or 4 bytes, but that it requires
* 9.
* So it throws a warning in compile time. But in this case we are
* actually testing that snprintf's return value is what it should be
* while truncating the output. So let's suppress this warning here.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"
#endif

int len;
char buffer[100];


/*******************/
buffer[0] = '\0';
len = snprintf(buffer, 0, "%x", DEADBEEF);
Expand All @@ -302,6 +314,9 @@ void test_snprintf(void)
"snprintf(%%x). Expected '%s', got '%s'\n",
"dea", buffer);

#if defined(__GNUC__) && __GNUC__ >= 7
#pragma GCC diagnostic pop
#endif
}

/**
Expand Down

0 comments on commit 2b02f8d

Please sign in to comment.