Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
Test change only: fix silly strncpy() compilation error for ESP-IDF/G…
Browse files Browse the repository at this point in the history
…CC 8. (#1167)

With later versions of GCC, as used by later versions of ESP-IDF, an exceptionally irritating new error has been introduced:

error: 'strncpy' specified bound equals destination size [-Werror=stringop-truncation]

This will occur anywhere that the compiler knows (a) the size of the destination buffer and (b) the value of "n", and the two are the same (e.g. likely because "n" is sizeof(buffer)).  This is extremely irritating because, despite the fact that your buffers are correctly dimensioned and the things you are copying are genuine strings, you have to reduce "n" by one and add another pointless line of code putting a terminator on the end of an already-terminated string.

Anyway, enough moaning: in the three cases in the test code where this error is emitted strncpy() is now replaced by memcpy().

Our thanks to jcradavelli for highlighting this issue.
  • Loading branch information
RobMeades authored May 23, 2024
1 parent 5e32f48 commit b645c90
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
17 changes: 13 additions & 4 deletions common/sock/test/u_sock_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ U_PORT_TEST_FUNCTION("[sock]", "sockAddressStrings")
uSockAddress_t address;
char *pAddress;
int32_t heapUsed;
size_t y;

// Whatever called us likely initialised the
// port so deinitialise it here to obtain the
Expand Down Expand Up @@ -842,8 +843,12 @@ U_PORT_TEST_FUNCTION("[sock]", "sockAddressStrings")

// Copy the address string into the buffer so that
// uSockDomainGetPort can write to it
strncpy(buffer, gTestAddressList[x].pAddressString,
sizeof(buffer));
// Note: using memcpy rather than strncpy here as GCC 8 emits a silly
// warning about sizeof(buffer) being the same size as the buffer
// otherwise.
y = strlen(gTestAddressList[x].pAddressString) + 1;
U_PORT_TEST_ASSERT(y <= sizeof(buffer));
memcpy(buffer, gTestAddressList[x].pAddressString, y);
if (gTestAddressList[x].hasPort) {
U_PORT_TEST_ASSERT(uSockDomainGetPort(buffer) == address.port);
// Now convert back to a string again
Expand Down Expand Up @@ -887,8 +892,12 @@ U_PORT_TEST_FUNCTION("[sock]", "sockAddressStrings")
// Test removing port numbers from an address string
for (size_t x = 0; x < sizeof(gTestAddressPortRemoval) /
sizeof(gTestAddressPortRemoval[0]); x++) {
strncpy(buffer, gTestAddressPortRemoval[x].pAddressStringOriginal,
sizeof(buffer));
// Note: using memcpy rather than strncpy here as GCC 8 emits a silly
// warning about sizeof(buffer) being the same size as the buffer
// otherwise.
y = strlen(gTestAddressPortRemoval[x].pAddressStringOriginal) + 1;
U_PORT_TEST_ASSERT(y <= sizeof(buffer));
memcpy(buffer, gTestAddressPortRemoval[x].pAddressStringOriginal, y);
U_TEST_PRINT_LINE("%d: original address string \"%s\""
" expected port number %d,"
" expected address string after port removal \"%s\".",
Expand Down
6 changes: 5 additions & 1 deletion gnss/test/u_gnss_private_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,11 @@ U_PORT_TEST_FUNCTION("[gnss]", "gnssPrivateNmea")
talkerSentenceBuffer, messageSize));

// Then with a wrong message ID
strncpy(talkerSentenceBuffer, gNmeaTestMessage[x].pTalkerSentenceStr, sizeof(talkerSentenceBuffer));
// Note: using memcpy rather than strncpy here as GCC 8 emits a silly warning about
// sizeof(talkerSentenceBuffer) being the same size as the buffer otherwise.
z = strlen(gNmeaTestMessage[x].pTalkerSentenceStr) + 1;
U_PORT_TEST_ASSERT(z <= sizeof(talkerSentenceBuffer));
memcpy(talkerSentenceBuffer, gNmeaTestMessage[x].pTalkerSentenceStr, z);
z = rand() % strlen(talkerSentenceBuffer);
talkerSentenceBuffer[z] = '_';
// Ensure terminator
Expand Down

0 comments on commit b645c90

Please sign in to comment.