Skip to content

Compiler error (gcc-16.1.1) about vcgencmd with strncat #197

@solsticedhiver

Description

@solsticedhiver

When using your code, but compiled with other flags, one gets the error:

In function 'gencmd',
    inlined from 'rpi_temp' at p/rpi_temp.c:156:15:
p/rpi_temp.c:124:5: error: 'strncat' output may be truncated copying 4096 bytes from a string of length 4099 [-Werror=stringop-truncation]
  124 |     strncat(result, (const char *)(p+6), result_len-1);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

line 113 in vcgencmd.c is the problem, some AI suggest something safer like

diff --git a/vcgencmd/vcgencmd.c b/vcgencmd/vcgencmd.c
index 63faf2e..bc5725c 100644
--- a/vcgencmd/vcgencmd.c
+++ b/vcgencmd/vcgencmd.c
@@ -109,8 +109,8 @@ static unsigned gencmd(int file_desc, const char *command, char *result, int res
    p[0] = i*sizeof *p; // actual size
 
    mbox_property(file_desc, p);
-   result[0] = 0;
-   strncat(result, (const char *)(p+6), result_len);
+   // Safely copies up to result_len - 1 characters and guarantees a \0 at the end
+   snprintf(result, result_len, "%s", (const char *)(p+6));
 
    return p[5];
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions