Skip to content

Commit a67f5e2

Browse files
committed
Fix #1173, separate append on volume_name to system_mountpt
Generating the system_mountpt string via a single call to snprintf triggered a compiler warning about overlapping memory. However this does not seem like a real warning, as the volume_name should always be null terminated before the overlap would be possible. By separating this to be a separate append of the volume_name along with an explicit size check to ensure the buffers indeed do not overlap, this avoids the warning.
1 parent a9fece5 commit a67f5e2

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/os/posix/src/os-impl-filesys.c

+21-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "os-posix.h"
4444
#include "os-shared-filesys.h"
4545
#include "os-shared-idmap.h"
46+
#include "os-shared-common.h"
4647

4748
/****************************************************************************************
4849
DEFINES
@@ -84,6 +85,8 @@ int32 OS_FileSysStartVolume_Impl(const OS_object_token_t *token)
8485
OS_filesys_internal_record_t *local;
8586
struct stat stat_buf;
8687
const char * tmpdir;
88+
size_t mplen;
89+
size_t vollen;
8790
uint32 i;
8891
enum
8992
{
@@ -168,7 +171,24 @@ int32 OS_FileSysStartVolume_Impl(const OS_object_token_t *token)
168171
return OS_FS_ERR_DRIVE_NOT_CREATED;
169172
}
170173

171-
snprintf(local->system_mountpt, sizeof(local->system_mountpt), "%s/osal:%s", tmpdir, local->volume_name);
174+
/*
175+
* Note - performing the concatenation in a single snprintf() call seems
176+
* to trigger a (false) pointer overlap warning, because volume_name should
177+
* always be null terminated. To get around this, calculate the
178+
* string size and check that it is within the expected size, and do the
179+
* append of volume_name explicitly.
180+
*/
181+
mplen = snprintf(local->system_mountpt, sizeof(local->system_mountpt), "%s/osal:", tmpdir);
182+
if (mplen < sizeof(local->system_mountpt))
183+
{
184+
vollen = OS_strnlen(local->volume_name, sizeof(local->volume_name));
185+
if ((vollen + mplen) >= sizeof(local->system_mountpt))
186+
{
187+
vollen = sizeof(local->system_mountpt) - mplen - 1;
188+
}
189+
memcpy(&local->system_mountpt[mplen], local->volume_name, vollen);
190+
local->system_mountpt[mplen + vollen] = 0;
191+
}
172192
}
173193

174194
return OS_SUCCESS;

0 commit comments

Comments
 (0)