Compiler error (gcc-16.1.1) about vcgencmd with strncat

Open Beginner friendly
#197 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
c
Domain
cli, embedded-iot

Research direction

Start in vcgencmd/vcgencmd.c around the gencmd function and reproduce the gcc-16.1.1 build failure with warnings treated as errors. Check the suggested bounded-copy change against the existing result buffer behavior. Done means the build no longer reports the strncat truncation warning while the command output remains safely terminated.

Written by the indexing model from the issue text.

Description

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];
 }
Dominant language
C
Stars
346
Forks
98
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from raspberrypi/utils

All issues in raspberrypi/utils

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.