diff options
author | Frej Drejhammar <frej.drejhammar@gmail.com> | 2023-04-11 10:24:32 +0200 |
---|---|---|
committer | Frej Drejhammar <frej.drejhammar@gmail.com> | 2023-05-02 16:10:10 +0200 |
commit | ba763d9dc2979d824006d96f182ebdb562191e5f (patch) | |
tree | 532752b7dfe3a24ba625afb5390c5ee8ef5d80fb | |
parent | e182323919b8ff695e48e5c5648306716482a3c4 (diff) | |
download | erlang-ba763d9dc2979d824006d96f182ebdb562191e5f.tar.gz |
escript: Avoid warning about truncated output in strncpy
GCC warns about how `strncpy` is used in `erts/etc/common/escript.c`:
```
escript.c: In function ‘main’:
escript.c:289:17: warning: ‘__builtin_strncpy’ output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]
289 | strncpy(dir, beg, sz);
| ^
In function ‘find_prog’,
inlined from ‘main’ at escript.c:483:25:
escript.c:282:26: note: length computed here
282 | sz = strlen(beg);
```
The warning is triggered as GCC understands that, as `sz =
strlen(beg)`, `strncpy` will never see and copy a terminating nul
character to `dir`.
As we manually null-terminate `dir`, by `dir[sz] = '\0';` on the line
following `strncpy` we can avoid the warning, and speed up the copy a
little (there is no need to look for a nul terminator), by using a
plain `memcpy`.
-rw-r--r-- | erts/etc/common/escript.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/erts/etc/common/escript.c b/erts/etc/common/escript.c index fe474338fb..c7418b2ace 100644 --- a/erts/etc/common/escript.c +++ b/erts/etc/common/escript.c @@ -286,7 +286,7 @@ find_prog(char *origpath) beg = end + 1; continue; } - strncpy(dir, beg, sz); + memcpy(dir, beg, sz); dir[sz] = '\0'; beg = end + 1; |