Skip to content

Commit

Permalink
Fix #805, Null terminate when using strncpy
Browse files Browse the repository at this point in the history
  • Loading branch information
skliper committed Feb 12, 2021
1 parent 920fde8 commit a8e0955
Show file tree
Hide file tree
Showing 20 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/os/shared/src/osapi-binsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
{
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);

strncpy(bin_prop->name, record->name_entry, OS_MAX_API_NAME - 1);
strncpy(bin_prop->name, record->name_entry, sizeof(bin_prop->name) - 1);
bin_prop->creator = record->creator;
return_code = OS_BinSemGetInfo_Impl(&token, bin_prop);

Expand Down
2 changes: 1 addition & 1 deletion src/os/shared/src/osapi-countsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ int32 OS_CountSemGetInfo(osal_id_t sem_id, OS_count_sem_prop_t *count_prop)
{
record = OS_OBJECT_TABLE_GET(OS_global_count_sem_table, token);

strncpy(count_prop->name, record->name_entry, OS_MAX_API_NAME - 1);
strncpy(count_prop->name, record->name_entry, sizeof(count_prop->name) - 1);
count_prop->creator = record->creator;

return_code = OS_CountSemGetInfo_Impl(&token, count_prop);
Expand Down
3 changes: 2 additions & 1 deletion src/os/shared/src/osapi-errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ int32 OS_GetErrorName(int32 error_num, os_err_name_t *err_name)

if (Error->Number == error_num && Error->Name != NULL)
{
strncpy(*err_name, Error->Name, OS_ERROR_NAME_LENGTH - 1);
strncpy(*err_name, Error->Name, sizeof(*err_name) - 1);
*err_name[sizeof(*err_name) - 1] = 0;
return_code = OS_SUCCESS;
}
else
Expand Down
5 changes: 3 additions & 2 deletions src/os/shared/src/osapi-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ int32 OS_rename(const char *old, const char *new)

if (stream->socket_domain == OS_SocketDomain_INVALID && strcmp(stream->stream_name, old) == 0)
{
strcpy(stream->stream_name, new);
strncpy(stream->stream_name, new, sizeof(stream->stream_name) - 1);
stream->stream_name[sizeof(stream->stream_name) - 1] = 0;
}
}

Expand Down Expand Up @@ -606,7 +607,7 @@ int32 OS_FDGetInfo(osal_id_t filedes, OS_file_prop_t *fd_prop)
{
record = OS_OBJECT_TABLE_GET(OS_global_stream_table, token);

strncpy(fd_prop->Path, record->name_entry, OS_MAX_PATH_LEN - 1);
strncpy(fd_prop->Path, record->name_entry, sizeof(fd_prop->Path) - 1);
fd_prop->User = record->creator;
fd_prop->IsValid = true;

Expand Down
5 changes: 3 additions & 2 deletions src/os/shared/src/osapi-filesys.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ int32 OS_FileSys_Initialize(char *address, const char *fsdevname, const char *fs
filesys->blocksize = blocksize;
filesys->numblocks = numblocks;
filesys->address = address;
strcpy(filesys->volume_name, fsvolname);
strncpy(filesys->volume_name, fsvolname, sizeof(filesys->volume_name) - 1);

/*
* Determine basic type of filesystem, if not already known
Expand Down Expand Up @@ -461,7 +461,8 @@ int32 OS_mount(const char *devname, const char *mountpoint)
/* mark as mounted in the local table.
* For now this does both sides (system and virtual) */
filesys->flags |= OS_FILESYS_FLAG_IS_MOUNTED_SYSTEM | OS_FILESYS_FLAG_IS_MOUNTED_VIRTUAL;
strcpy(filesys->virtual_mountpt, mountpoint);
strncpy(filesys->virtual_mountpt, mountpoint, sizeof(filesys->virtual_mountpt) - 1);
filesys->virtual_mountpt[sizeof(filesys->virtual_mountpt) - 1] = 0;
}

OS_ObjectIdRelease(&token);
Expand Down
6 changes: 3 additions & 3 deletions src/os/shared/src/osapi-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ int32 OS_ModuleLoad(osal_id_t *module_id, const char *module_name, const char *f
else
{
/* supplied filename was valid, so store a copy for future reference */
strncpy(module->file_name, filename, OS_MAX_PATH_LEN);
strncpy(module->file_name, filename, sizeof(module->file_name) - 1);
module->module_type = OS_MODULE_TYPE_DYNAMIC;

/* Now call the OS-specific implementation. This reads info from the module table. */
Expand Down Expand Up @@ -333,8 +333,8 @@ int32 OS_ModuleInfo(osal_id_t module_id, OS_module_prop_t *module_prop)
record = OS_OBJECT_TABLE_GET(OS_global_module_table, token);
module = OS_OBJECT_TABLE_GET(OS_module_table, token);

strncpy(module_prop->name, record->name_entry, OS_MAX_API_NAME - 1);
strncpy(module_prop->filename, module->file_name, OS_MAX_API_NAME - 1);
strncpy(module_prop->name, record->name_entry, sizeof(module_prop->name) - 1);
strncpy(module_prop->filename, module->file_name, sizeof(module_prop->filename) - 1);

return_code = OS_ModuleGetInfo_Impl(&token, module_prop);

Expand Down
2 changes: 1 addition & 1 deletion src/os/shared/src/osapi-mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ int32 OS_MutSemGetInfo(osal_id_t sem_id, OS_mut_sem_prop_t *mut_prop)
{
record = OS_OBJECT_TABLE_GET(OS_global_mutex_table, token);

strncpy(mut_prop->name, record->name_entry, OS_MAX_API_NAME - 1);
strncpy(mut_prop->name, record->name_entry, sizeof(mut_prop->name) - 1);
mut_prop->creator = record->creator;

return_code = OS_MutSemGetInfo_Impl(&token, mut_prop);
Expand Down
2 changes: 1 addition & 1 deletion src/os/shared/src/osapi-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ int32 OS_QueueGetInfo(osal_id_t queue_id, OS_queue_prop_t *queue_prop)
{
record = OS_OBJECT_TABLE_GET(OS_global_queue_table, token);

strncpy(queue_prop->name, record->name_entry, OS_MAX_API_NAME - 1);
strncpy(queue_prop->name, record->name_entry, sizeof(queue_prop->name) - 1);
queue_prop->creator = record->creator;

/*
Expand Down
2 changes: 1 addition & 1 deletion src/os/shared/src/osapi-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ int32 OS_SocketGetInfo(osal_id_t sock_id, OS_socket_prop_t *sock_prop)
{
record = OS_OBJECT_TABLE_GET(OS_global_stream_table, token);

strncpy(sock_prop->name, record->name_entry, OS_MAX_API_NAME - 1);
strncpy(sock_prop->name, record->name_entry, sizeof(sock_prop->name) - 1);
sock_prop->creator = record->creator;
return_code = OS_SocketGetInfo_Impl(&token, sock_prop);

Expand Down
2 changes: 1 addition & 1 deletion src/os/shared/src/osapi-time.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ int32 OS_TimerGetInfo(osal_id_t timer_id, OS_timer_prop_t *timer_prop)
timecb = OS_OBJECT_TABLE_GET(OS_timecb_table, token);
timebase = OS_OBJECT_TABLE_GET(OS_timebase_table, timecb->timebase_token);

strncpy(timer_prop->name, record->name_entry, OS_MAX_API_NAME - 1);
strncpy(timer_prop->name, record->name_entry, sizeof(timer_prop->name) - 1);
timer_prop->creator = record->creator;
timer_prop->interval_time = (uint32)timecb->interval_time;
timer_prop->accuracy = timebase->accuracy_usec;
Expand Down
2 changes: 1 addition & 1 deletion src/os/shared/src/osapi-timebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ int32 OS_TimeBaseGetInfo(osal_id_t timebase_id, OS_timebase_prop_t *timebase_pro
record = OS_OBJECT_TABLE_GET(OS_global_timebase_table, token);
timebase = OS_OBJECT_TABLE_GET(OS_timebase_table, token);

strncpy(timebase_prop->name, record->name_entry, OS_MAX_API_NAME - 1);
strncpy(timebase_prop->name, record->name_entry, sizeof(timebase_prop->name) - 1);
timebase_prop->creator = record->creator;
timebase_prop->nominal_interval_time = timebase->nominal_interval_time;
timebase_prop->freerun_time = timebase->freerun_time;
Expand Down
3 changes: 2 additions & 1 deletion src/os/vxworks/src/os-impl-symtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ BOOL OS_SymTableIterator_Impl(char *name, SYM_VALUE val, SYM_TYPE type, _Vx_usr_
/*
** Copy symbol name
*/
strncpy(symRecord.SymbolName, name, OS_MAX_SYM_LEN);
strncpy(symRecord.SymbolName, name, sizeof(symRecord.SymbolName) - 1);
symRecord.SymbolName[sizeof(symRecord.SymbolName) - 1] = 0;

/*
** Save symbol address
Expand Down
4 changes: 2 additions & 2 deletions src/ut-stubs/osapi-utstub-binsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
UT_Stub_CopyToLocal(UT_KEY(OS_BinSemGetInfo), bin_prop, sizeof(*bin_prop)) < sizeof(*bin_prop))
{
UT_ObjIdCompose(1, OS_OBJECT_TYPE_OS_TASK, &bin_prop->creator);
strncpy(bin_prop->name, "Name", OS_MAX_API_NAME - 1);
bin_prop->name[OS_MAX_API_NAME - 1] = '\0';
strncpy(bin_prop->name, "Name", sizeof(bin_prop->name) - 1);
bin_prop->name[sizeof(bin_prop->name) - 1] = '\0';
}

return status;
Expand Down
4 changes: 2 additions & 2 deletions src/ut-stubs/osapi-utstub-countsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ int32 OS_CountSemGetInfo(osal_id_t sem_id, OS_count_sem_prop_t *count_prop)
UT_Stub_CopyToLocal(UT_KEY(OS_CountSemGetInfo), count_prop, sizeof(*count_prop)) < sizeof(*count_prop))
{
UT_ObjIdCompose(1, OS_OBJECT_TYPE_OS_TASK, &count_prop->creator);
strncpy(count_prop->name, "Name", OS_MAX_API_NAME - 1);
count_prop->name[OS_MAX_API_NAME - 1] = '\0';
strncpy(count_prop->name, "Name", sizeof(count_prop->name) - 1);
count_prop->name[sizeof(count_prop->name) - 1] = '\0';
}

return status;
Expand Down
6 changes: 4 additions & 2 deletions src/ut-stubs/osapi-utstub-filesys.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ int32 OS_FS_GetPhysDriveName(char *PhysDriveName, const char *MountPoint)
int32 status;

status = UT_DEFAULT_IMPL(OS_FS_GetPhysDriveName);
strncpy(PhysDriveName, MountPoint, OS_FS_PHYS_NAME_LEN);
strncpy(PhysDriveName, MountPoint, OS_FS_PHYS_NAME_LEN - 1);
PhysDriveName[OS_FS_PHYS_NAME_LEN - 1] = 0;

return status;
}
Expand Down Expand Up @@ -288,7 +289,8 @@ int32 OS_TranslatePath(const char *VirtualPath, char *LocalPath)
if (status == OS_SUCCESS && VirtualPath != NULL && LocalPath != NULL &&
UT_Stub_CopyToLocal(UT_KEY(OS_TranslatePath), LocalPath, OS_MAX_LOCAL_PATH_LEN) == 0)
{
strncpy(LocalPath, VirtualPath, OS_MAX_LOCAL_PATH_LEN);
strncpy(LocalPath, VirtualPath, OS_MAX_LOCAL_PATH_LEN - 1);
LocalPath[OS_MAX_LOCAL_PATH_LEN - 1] = 0;
}

return status;
Expand Down
4 changes: 2 additions & 2 deletions src/ut-stubs/osapi-utstub-mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ int32 OS_MutSemGetInfo(osal_id_t sem_id, OS_mut_sem_prop_t *mut_prop)
if (status == OS_SUCCESS &&
UT_Stub_CopyToLocal(UT_KEY(OS_MutSemGetInfo), mut_prop, sizeof(*mut_prop)) < sizeof(*mut_prop))
{
strncpy(mut_prop->name, "Name", OS_MAX_API_NAME - 1);
mut_prop->name[OS_MAX_API_NAME - 1] = '\0';
strncpy(mut_prop->name, "Name", sizeof(mut_prop->name) - 1);
mut_prop->name[sizeof(mut_prop->name) - 1] = '\0';
UT_ObjIdCompose(1, OS_OBJECT_TYPE_OS_TASK, &mut_prop->creator);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ut-stubs/osapi-utstub-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ int32 OS_QueueGetInfo(osal_id_t queue_id, OS_queue_prop_t *queue_prop)
UT_Stub_CopyToLocal(UT_KEY(OS_QueueGetInfo), queue_prop, sizeof(*queue_prop)) < sizeof(*queue_prop))
{
UT_ObjIdCompose(1, OS_OBJECT_TYPE_OS_TASK, &queue_prop->creator);
strncpy(queue_prop->name, "Name", OS_MAX_API_NAME - 1);
queue_prop->name[OS_MAX_API_NAME - 1] = '\0';
strncpy(queue_prop->name, "Name", sizeof(queue_prop->name) - 1);
queue_prop->name[sizeof(queue_prop->name) - 1] = '\0';
}

return status;
Expand Down
3 changes: 2 additions & 1 deletion src/ut-stubs/osapi-utstub-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ int32 OS_SocketGetInfo(osal_id_t sock_id, OS_socket_prop_t *sock_prop)
if (CopySize < sizeof(*sock_prop))
{
UT_ObjIdCompose(1, OS_OBJECT_TYPE_OS_TASK, &sock_prop->creator);
strncpy(sock_prop->name, "ut", sizeof(sock_prop->name));
strncpy(sock_prop->name, "ut", sizeof(sock_prop->name) - 1);
sock_prop->name[sizeof(sock_prop->name) - 1] = 0;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ut-stubs/osapi-utstub-task.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ int32 OS_TaskGetInfo(osal_id_t task_id, OS_task_prop_t *task_prop)
UT_ObjIdCompose(1, OS_OBJECT_TYPE_OS_TASK, &task_prop->creator);
task_prop->stack_size = OSAL_SIZE_C(100);
task_prop->priority = OSAL_PRIORITY_C(150);
strncpy(task_prop->name, "UnitTest", OS_MAX_API_NAME - 1);
task_prop->name[OS_MAX_API_NAME - 1] = '\0';
strncpy(task_prop->name, "UnitTest", sizeof(task_prop->name) - 1);
task_prop->name[sizeof(task_prop->name) - 1] = '\0';
}

return status;
Expand Down
4 changes: 2 additions & 2 deletions src/ut-stubs/osapi-utstub-timebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ int32 OS_TimeBaseGetInfo(osal_id_t timebase_id, OS_timebase_prop_t *timebase_pro
UT_Stub_CopyToLocal(UT_KEY(OS_TimeBaseGetInfo), timebase_prop, sizeof(*timebase_prop)) < sizeof(*timebase_prop))
{
UT_ObjIdCompose(1, OS_OBJECT_TYPE_OS_TASK, &timebase_prop->creator);
strncpy(timebase_prop->name, "Name", OS_MAX_API_NAME - 1);
timebase_prop->name[OS_MAX_API_NAME - 1] = '\0';
strncpy(timebase_prop->name, "Name", sizeof(timebase_prop->name) - 1);
timebase_prop->name[sizeof(timebase_prop->name) - 1] = '\0';
}

return status;
Expand Down

0 comments on commit a8e0955

Please sign in to comment.