From b5370af822c8e81ed2248ac8aa3bb479a0db9dae Mon Sep 17 00:00:00 2001 From: Far Date: Mon, 7 Jun 2021 16:49:02 +0800 Subject: [PATCH] fix: codex clean MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修复可能对NULL指针解引用的场景 2. 将不修改内容的指针入参修改为const 3. 对getpgrp的返回值进行校验后再使用 4. 修复了局部变量未初始化的问题 Close #I3UOFN Signed-off-by: Far --- apps/init/src/init.c | 10 ++++++++-- apps/shell/src/shmsg.c | 10 ++++++++-- arch/arm/arm/src/include/los_hw_pri.h | 2 +- arch/arm/arm/src/los_hw.c | 2 +- fs/vfs/disk/disk.c | 9 ++++++--- kernel/common/console.c | 6 +++--- 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/apps/init/src/init.c b/apps/init/src/init.c index c13f24a2..39de64d3 100644 --- a/apps/init/src/init.c +++ b/apps/init/src/init.c @@ -49,6 +49,7 @@ int main(int argc, char * const *argv) { int ret; + pid_t gid; const char *shellPath = "/bin/mksh"; #ifdef LOSCFG_QUICK_START @@ -74,9 +75,14 @@ int main(int argc, char * const *argv) if (ret < 0) { printf("Failed to fork for shell\n"); } else if (ret == 0) { - ret = tcsetpgrp(STDIN_FILENO, getpgrp()); + gid = getpgrp(); + if (gid < 0) { + printf("get group id failed, pgrpid %d, errno %d\n", gid, errno); + exit(0); + } + ret = tcsetpgrp(STDIN_FILENO, gid); if (ret != 0) { - printf("tcsetpgrp failed, pgrpid %d, errno %d\n", getpgrp(), errno); + printf("tcsetpgrp failed, errno %d\n", errno); exit(0); } (void)execve(shellPath, NULL, NULL); diff --git a/apps/shell/src/shmsg.c b/apps/shell/src/shmsg.c index 1640810d..b4f237ba 100644 --- a/apps/shell/src/shmsg.c +++ b/apps/shell/src/shmsg.c @@ -333,6 +333,7 @@ static void DoCmdExec(const char *cmdName, const char *cmdline, unsigned int len { int ret; pid_t forkPid; + pid_t gid; if (strncmp(cmdline, SHELL_EXEC_COMMAND, SHELL_EXEC_COMMAND_BYTES) == 0) { forkPid = fork(); @@ -345,9 +346,14 @@ static void DoCmdExec(const char *cmdName, const char *cmdline, unsigned int len exit(1); } - ret = tcsetpgrp(STDIN_FILENO, getpgrp()); + gid = getpgrp(); + if (gid < 0) { + printf("get group id failed, pgrpid %d, errno %d\n", gid, errno); + } + + ret = tcsetpgrp(STDIN_FILENO, gid); if (ret != 0) { - printf("tcsetpgrp failed, pgrpid %d, errno %d\n", getpgrp(), errno); + printf("tcsetpgrp failed, errno %d\n", errno); } ret = execve((const char *)cmdParsed->paramArray[0], (char * const *)cmdParsed->paramArray, NULL); diff --git a/arch/arm/arm/src/include/los_hw_pri.h b/arch/arm/arm/src/include/los_hw_pri.h index d4f5a880..d764cc9b 100644 --- a/arch/arm/arm/src/include/los_hw_pri.h +++ b/arch/arm/arm/src/include/los_hw_pri.h @@ -104,7 +104,7 @@ typedef struct { extern VOID *OsTaskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack, BOOL initFlag); extern VOID OsUserCloneParentStack(VOID *childStack, UINTPTR parentTopOfStask, UINT32 parentStackSize); extern VOID OsUserTaskStackInit(TaskContext *context, UINTPTR taskEntry, UINTPTR stack); -extern VOID OsInitSignalContext(VOID *sp, VOID *signalContext, UINTPTR sigHandler, UINT32 signo, UINT32 param); +extern VOID OsInitSignalContext(const VOID *sp, VOID *signalContext, UINTPTR sigHandler, UINT32 signo, UINT32 param); extern void arm_clean_cache_range(UINTPTR start, UINTPTR end); extern void arm_inv_cache_range(UINTPTR start, UINTPTR end); diff --git a/arch/arm/arm/src/los_hw.c b/arch/arm/arm/src/los_hw.c index 5af4068c..2db15a42 100644 --- a/arch/arm/arm/src/los_hw.c +++ b/arch/arm/arm/src/los_hw.c @@ -126,7 +126,7 @@ LITE_OS_SEC_TEXT_INIT VOID OsUserTaskStackInit(TaskContext *context, UINTPTR tas context->PC = (UINTPTR)taskEntry; } -VOID OsInitSignalContext(VOID *sp, VOID *signalContext, UINTPTR sigHandler, UINT32 signo, UINT32 param) +VOID OsInitSignalContext(const VOID *sp, VOID *signalContext, UINTPTR sigHandler, UINT32 signo, UINT32 param) { IrqContext *newSp = (IrqContext *)signalContext; (VOID)memcpy_s(signalContext, sizeof(IrqContext), sp, sizeof(IrqContext)); diff --git a/fs/vfs/disk/disk.c b/fs/vfs/disk/disk.c index 071b1d90..c131ffc3 100644 --- a/fs/vfs/disk/disk.c +++ b/fs/vfs/disk/disk.c @@ -865,7 +865,7 @@ INT32 los_disk_write(INT32 drvID, const VOID *buf, UINT64 sector, UINT32 count) #endif INT32 result = VFS_ERROR; los_disk *disk = get_disk(drvID); - if (disk == NULL) { + if (disk == NULL || disk->dev == NULL || disk->dev->data == NULL) { return result; } @@ -896,7 +896,7 @@ INT32 los_disk_write(INT32 drvID, const VOID *buf, UINT64 sector, UINT32 count) } else { #endif struct block_operations *bops = (struct block_operations *)((struct drv_data *)disk->dev->data)->ops; - if ((disk->dev != NULL) && (bops != NULL) && (bops->write != NULL)) { + if ((bops != NULL) && (bops->write != NULL)) { result = bops->write(disk->dev, (UINT8 *)buf, sector, count); if (result == (INT32)count) { result = ENOERR; @@ -1151,6 +1151,9 @@ INT32 los_disk_cache_clear(INT32 drvID) los_part *part = get_part(drvID); los_disk *disk = NULL; + if (part == NULL) { + return VFS_ERROR; + } result = OsSdSync(part->disk_id); if (result != 0) { PRINTK("[ERROR]disk_cache_clear SD sync failed!\n"); @@ -1159,7 +1162,7 @@ INT32 los_disk_cache_clear(INT32 drvID) disk = get_disk(part->disk_id); if (disk == NULL) { - return -1; + return VFS_ERROR; } DISK_LOCK(&disk->disk_mutex); diff --git a/kernel/common/console.c b/kernel/common/console.c index 584acc3e..96996012 100644 --- a/kernel/common/console.c +++ b/kernel/common/console.c @@ -171,7 +171,7 @@ BOOL IsConsoleOccupied(const CONSOLE_CB *consoleCB) STATIC INT32 ConsoleCtrlCaptureLine(CONSOLE_CB *consoleCB) { - struct termios consoleTermios; + struct termios consoleTermios = {0}; UINT32 intSave; LOS_SpinLockSave(&g_consoleSpin, &intSave); @@ -185,7 +185,7 @@ STATIC INT32 ConsoleCtrlCaptureLine(CONSOLE_CB *consoleCB) STATIC INT32 ConsoleCtrlCaptureChar(CONSOLE_CB *consoleCB) { - struct termios consoleTermios; + struct termios consoleTermios = {0}; UINT32 intSave; LOS_SpinLockSave(&g_consoleSpin, &intSave); @@ -1014,7 +1014,7 @@ STATIC const struct file_operations_vfs g_consoleDevOps = { STATIC VOID OsConsoleTermiosInit(CONSOLE_CB *consoleCB, const CHAR *deviceName) { - struct termios consoleTermios; + struct termios consoleTermios = {0}; if ((deviceName != NULL) && (strlen(deviceName) == strlen(SERIAL)) &&