Skip to content

Commit

Permalink
Corrected test_snprintf (#1079)
Browse files Browse the repository at this point in the history
Fixed a compiler warning in test_snprintf and a cppcheck warning in strutils.
  • Loading branch information
jesper-friis authored Jan 24, 2025
1 parent 4e2bc80 commit e15b76c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/utils/strutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ int strnquote(char *dest, size_t size, const char *s, int n,

/* If `s` is NULL, use system snprintf() to represent it in a standard
way. */
// cppcheck-suppress nullPointer
int m = snprintf(dest+i, PDIFF(size, i), "%s", s);
if (m >= 0) i += j;
}
Expand Down
8 changes: 5 additions & 3 deletions src/utils/tests/test_snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ MU_TEST(test_snprintf)
{
char buf[10];
char *short_string = "abc";
char *long_string = "0123456789abcdef";
char *long_string = strdup("0123456789abcdef");
int n;

printf("\n\n--- test_snprintf\n");
n = rpl_snprintf(buf, sizeof(buf), "%s", short_string);
n = snprintf(buf, sizeof(buf), "%s", short_string);
mu_assert_int_eq(strlen(short_string), n);

memset(buf, 0, sizeof(buf));
n = rpl_snprintf(buf, 4, "%s", long_string);
n = snprintf(buf, 4, "%s", long_string);
printf("\n*** n=%d, buf='%.10s'\n", n, buf);


n = snprintf(buf, sizeof(buf), "%s", long_string);
mu_assert_int_eq(strlen(long_string), n);

free(long_string);
}


Expand Down

0 comments on commit e15b76c

Please sign in to comment.