Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix strncpy() boundary condition (triggers GCC 9.x warning) #262

Open
karlthewex opened this issue Feb 21, 2020 · 0 comments
Open

fix strncpy() boundary condition (triggers GCC 9.x warning) #262

karlthewex opened this issue Feb 21, 2020 · 0 comments

Comments

@karlthewex
Copy link

There are a few strncpy() invocations that could result in a string that is not NULL-terminated.

I see the warning using SPIFFS V0.3.7

Here's a git diff showing the suggested change that was applied locally.

diff --git a/source/third_party/spiffs/spiffs_hydrogen.c b/source/third_party/spiffs/spiffs_hydrogen.c
index 23c305a45..97a445f30 100644
--- a/source/third_party/spiffs/spiffs_hydrogen.c
+++ b/source/third_party/spiffs/spiffs_hydrogen.c
@@ -856,6 +856,7 @@ static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spi
s->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
s->pix = pix;
strncpy((char *)s->name, (char *)objix_hdr.name, SPIFFS_OBJ_NAME_LEN);

  • s->name[SPIFFS_OBJ_NAME_LEN-1] = '\0'; // ensure string is NULL terminated
    #if SPIFFS_OBJ_META_LEN
    _SPIFFS_MEMCPY(s->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN);
    #endif
    diff --git a/source/third_party/spiffs/spiffs_nucleus.c b/source/third_party/spiffs/spiffs_nucleus.c
    index 6b052b0e3..88bd55cab 100644
    --- a/source/third_party/spiffs/spiffs_nucleus.c
    +++ b/source/third_party/spiffs/spiffs_nucleus.c
    @@ -945,6 +945,7 @@ s32_t spiffs_object_create(
    oix_hdr.type = type;
    oix_hdr.size = SPIFFS_UNDEFINED_LEN; // keep ones so we can update later without wasting this page
    strncpy((char*)oix_hdr.name, (const char*)name, SPIFFS_OBJ_NAME_LEN);
  • oix_hdr.name[SPIFFS_OBJ_NAME_LEN-1] = '\0'; // ensure string is NULL terminated
    #if SPIFFS_OBJ_META_LEN
    if (meta) {
    _SPIFFS_MEMCPY(oix_hdr.meta, meta, SPIFFS_OBJ_META_LEN);
    @@ -1008,6 +1009,7 @@ s32_t spiffs_object_update_index_hdr(
    // change name
    if (name) {
    strncpy((char*)objix_hdr->name, (const char*)name, SPIFFS_OBJ_NAME_LEN);
  • objix_hdr->name[SPIFFS_OBJ_NAME_LEN-1] = '\0'; // ensure string is NULL terminated
    }
    #if SPIFFS_OBJ_META_LEN
    if (meta) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant