Skip to content

Commit 7c8d538

Browse files
aescolarcarlescufi
authored andcommitted
posix: env: Fix 2 build warnings
Fix 2 build warnings in posix/options/env: The maximum length given to strncpy() matches the input string length, which makes the call equivalent to strcpy(). As the destination buffer size has been ensured sufficient (in the first case by chechking just before, in the second case by allocating it big enough), let's just use strcpy() instead. lib/posix/options/env.c: In function 'getenv_r': lib/posix/options/env.c:109:17: error: 'strncpy' specified bound depends on the length of the source argument [-Werror=stringop-truncation] 109 | strncpy(buf, val, vsize); | ^~~~~~~~~~~~~~~~~~~~~~~~ lib/posix/options/env.c:104:25: note: length computed here 104 | vsize = strlen(val) + 1; | ^~~~~~~~~~~ lib/posix/options/env.c: In function 'setenv': lib/posix/options/env.c:191:17: error: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation] 191 | strncpy(environ[ret], name, nsize); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lib/posix/options/env.c:128:51: note: length computed here 128 | const size_t nsize = (name == NULL) ? 0 : strlen(name); | ^~~~~~~~~~~~ Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
1 parent f84e082 commit 7c8d538

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

lib/posix/options/env.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ int getenv_r(const char *name, char *buf, size_t len)
106106
ret = -ERANGE;
107107
K_SPINLOCK_BREAK;
108108
}
109-
strncpy(buf, val, vsize);
109+
strcpy(buf, val);
110110
LOG_DBG("Found entry %s", environ[ret]);
111111
}
112112

@@ -188,7 +188,7 @@ int setenv(const char *name, const char *val, int overwrite)
188188
environ[ret] = env;
189189
}
190190

191-
strncpy(environ[ret], name, nsize);
191+
strcpy(environ[ret], name);
192192
environ[ret][nsize] = '=';
193193
strncpy(environ[ret] + nsize + 1, val, vsize + 1);
194194
LOG_DBG("Added entry %s", environ[ret]);

0 commit comments

Comments
 (0)