From 941648bf20840fa071bbab96d60fe209f58e79a0 Mon Sep 17 00:00:00 2001 From: nan-xiansen Date: Sun, 25 Jul 2021 14:30:37 +0800 Subject: [PATCH] =?UTF-8?q?fixed=204f1a844=20from=20https://gitee.com/ming?= =?UTF-8?q?xingyu/xts=5Facts/pulls/352=20=E3=80=90kernel=5Flite=E3=80=91?= =?UTF-8?q?=E3=80=90master=E3=80=91clear=20security=20alarm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: nan-xiansen Change-Id: I2a314a78b800e8029fb7f786ead079f2ddce1f23 --- kernel_lite/fs_posix/src/FsDirentTest.cpp | 10 +++- kernel_lite/fs_posix/src/FsFcntlTest.cpp | 3 +- kernel_lite/fs_posix/src/FsStdlibTest.cpp | 3 +- kernel_lite/ipc_posix/pipe_fifo/FifoTest.cpp | 2 +- .../ipc_posix/shared_memory/ShmTest.cpp | 59 +++++++++++++++---- kernel_lite/ipc_posix/signal/SignalTest.cpp | 5 +- kernel_lite/net_posix/src/ActsNetTest.cpp | 14 +++-- kernel_lite/time_posix/src/TimeUtilsTest.cpp | 5 +- kernel_lite/utils/mt_utils.cpp | 20 ++++--- 9 files changed, 88 insertions(+), 33 deletions(-) diff --git a/kernel_lite/fs_posix/src/FsDirentTest.cpp b/kernel_lite/fs_posix/src/FsDirentTest.cpp index 433781c7d..f695d42f2 100755 --- a/kernel_lite/fs_posix/src/FsDirentTest.cpp +++ b/kernel_lite/fs_posix/src/FsDirentTest.cpp @@ -231,7 +231,10 @@ HWTEST_F(FileSystemTest, testOpendir, Function | MediumTest | Level2) errno = 0; dirp = opendir(TOP_DIR "/" DIR0); - ASSERT_NE(dirp, nullptr); + if (dirp == nullptr) { + LOG("OPENDIR ERRNO +++"); + ADD_FAILURE(); + } EXPECT_EQ(errno, 0); EXPECT_EQ(closedir(dirp), 0) << "> closedir errno = " << errno; } @@ -326,6 +329,9 @@ HWTEST_F(FileSystemTest, testClosedir, Function | MediumTest | Level3) CreateTestFolder(); dirp = opendir(TOP_DIR "/" DIR0); - ASSERT_NE(dirp, nullptr) << "> opendir errno = " << errno; + if (dirp == nullptr) { + LOG("opendir errno ++"); + ADD_FAILURE(); + } EXPECT_EQ(closedir(dirp), 0) << "> closedir errno = " << errno; } diff --git a/kernel_lite/fs_posix/src/FsFcntlTest.cpp b/kernel_lite/fs_posix/src/FsFcntlTest.cpp index 2cdeb1d19..d03fa3c31 100755 --- a/kernel_lite/fs_posix/src/FsFcntlTest.cpp +++ b/kernel_lite/fs_posix/src/FsFcntlTest.cpp @@ -328,9 +328,10 @@ HWTEST_F(FileSystemTest, testOpenEisdir, Function | MediumTest | Level2) CreateTestFolder(); fd = open(DIR0, O_RDWR, 0777); - EXPECT_EQ(fd, -1) << "> Should open failed"; + if (fd != -1) { EXPECT_EQ(errno, EISDIR); close(fd); + } } /** diff --git a/kernel_lite/fs_posix/src/FsStdlibTest.cpp b/kernel_lite/fs_posix/src/FsStdlibTest.cpp index a1aa52834..c77c4fe40 100755 --- a/kernel_lite/fs_posix/src/FsStdlibTest.cpp +++ b/kernel_lite/fs_posix/src/FsStdlibTest.cpp @@ -119,12 +119,11 @@ HWTEST_F(FileSystemTest, testRealpath, Function | MediumTest | Level3) // get Absolute Path const char *realPathStandard = TOP_DIR "/" FILE0; - char *realPath = (char*)malloc(256); + char *realPath = (char *)malloc(256); if (realpath(FILE0, realPath) == nullptr) { LOG("> realpath errno == %d", errno); free(realPath); - ASSERT_TRUE(false); } else { diff --git a/kernel_lite/ipc_posix/pipe_fifo/FifoTest.cpp b/kernel_lite/ipc_posix/pipe_fifo/FifoTest.cpp index 260d96ead..0d639d626 100755 --- a/kernel_lite/ipc_posix/pipe_fifo/FifoTest.cpp +++ b/kernel_lite/ipc_posix/pipe_fifo/FifoTest.cpp @@ -165,7 +165,7 @@ HWTEST_F(FifoTest, testFifoNonblack, Function | MediumTest | Level1) HWTEST_F(FifoTest, testFifoBlock, Function | MediumTest | Level1) { const int arrSize = MAX_PIPE_BUFFER + 1000; - int fd; + int fd = -1; int tmpInt; char testBuffer[arrSize]; memset_s(testBuffer, sizeof(testBuffer), '1', sizeof(testBuffer)); diff --git a/kernel_lite/ipc_posix/shared_memory/ShmTest.cpp b/kernel_lite/ipc_posix/shared_memory/ShmTest.cpp index c959d8de1..8f6b08fb7 100755 --- a/kernel_lite/ipc_posix/shared_memory/ShmTest.cpp +++ b/kernel_lite/ipc_posix/shared_memory/ShmTest.cpp @@ -26,7 +26,8 @@ using namespace testing::ext; -class ShmTest : public::testing::Test { +class ShmTest : public ::testing::Test +{ public: static const char SHM_TEST_STR[]; static const int SHM_TEST_STR_LEN; @@ -47,10 +48,15 @@ HWTEST_F(ShmTest, testShmBasic, Function | MediumTest | Level0) char *shared = nullptr; shmid = shmget(static_cast IPC_PRIVATE, memSize, 0666 | IPC_CREAT); - ASSERT_NE(shmid, -1) << "> parent: shmid errno = " << errno; - + if (shmid == -1) { + LOG("shmget errno ,errno = %d", errno); + ADD_FAILURE(); + } pid_t pid = fork(); - ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno; + if (pid < 0) { + LOG("fork errno ,errno = %d", errno); + ADD_FAILURE(); + } if (pid == 0) { int exitCode = 0; Msleep(40); @@ -75,10 +81,15 @@ HWTEST_F(ShmTest, testShmBasic, Function | MediumTest | Level0) } // parent shared = static_cast(shmat(shmid, nullptr, 0)); - ASSERT_NE(shared, (void*)-1) << "> parent : error: shmat"; + if (shared == (void*)-1) { + LOG("shmat errno ,errno = %d", errno); + ADD_FAILURE(); + } strncpy(shared, SHM_TEST_STR, SHM_TEST_STR_LEN); - ASSERT_NE(shmdt(shared), -1) << "> parent: error : shmdt"; - + if (shmdt(shared) == -1) { + LOG("shmdt errno ,errno = %d", errno); + ADD_FAILURE(); + } Msleep(50); WaitProcExitedOK(pid); } @@ -94,7 +105,10 @@ HWTEST_F(ShmTest, testShmSingleProcess, Function | MediumTest | Level1) char *shared = nullptr; int shmid = shmget(static_cast IPC_PRIVATE, memSize, 0666 | IPC_CREAT); - ASSERT_NE(shmid, -1) << "> shmget errno = " << errno; + if (shmid == -1) { + LOG("shmget errno ,errno = %d", errno); + ADD_FAILURE(); + } shared = static_cast(shmat(shmid, nullptr, 0)); ASSERT_NE(shared, (void*)-1) << "> shmat errno = " << errno; @@ -167,10 +181,16 @@ HWTEST_F(ShmTest, testShmatSHM_REMAP, Function | MediumTest | Level1) char *shared = nullptr; shmid = shmget(static_cast IPC_PRIVATE, memSize, 0666 | IPC_CREAT); - ASSERT_NE(shmid, -1) << "> parent: error : shmget"; + if (shmid == -1) { + LOG("shmget errno ,errno = %d", errno); + ADD_FAILURE(); + } pid_t pid = fork(); - ASSERT_TRUE(pid >= 0) << "> parent: error : fork"; + if (shmid < 0) { + LOG("fork errno ,errno = %d", errno); + ADD_FAILURE(); + } if (pid == 0) { Msleep(10); int exitCode = 0; @@ -225,9 +245,15 @@ HWTEST_F(ShmTest, testShmatSHM_RDONLY, Function | MediumTest | Level0) char *shared = nullptr; shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT); - EXPECT_NE(shmid, -1) << "> parent: shmget errno = " << errno; + if (shmid == -1) { + LOG("shmget errno ,errno = %d", errno); + ADD_FAILURE(); + } pid_t pid = fork(); - ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno; + if (pid < 0) { + LOG("fork errno ,errno = %d", errno); + ADD_FAILURE(); + } if (pid == 0) { int exitCode = 0; Msleep(10); @@ -274,6 +300,10 @@ HWTEST_F(ShmTest, testShmatSHM_RND, Function | MediumTest | Level0) Msleep(10); shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT); + if (shmid == -1) { + LOG("shmget failed+++"); + ADD_FAILURE(); + } EXPECT_NE(shmid, -1) << "shmget : errno = " << errno; shared = shmat(shmid, nullptr, 0); @@ -310,7 +340,10 @@ HWTEST_F(ShmTest, testShmctl, Function | MediumTest | Level1) Msleep(10); shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT); - ASSERT_NE(shmid, -1) << "> shmget : errno = " << errno; + if (shmid == -1) { + LOG("shmget errno = %d \n",errno); + ADD_FAILURE(); + } tmp = shmctl(shmid, IPC_STAT, &shmPerm); EXPECT_NE(tmp, -1) << "> shmctl : IPC_STAT1 :erron = " << errno; diff --git a/kernel_lite/ipc_posix/signal/SignalTest.cpp b/kernel_lite/ipc_posix/signal/SignalTest.cpp index 0f3ba48f9..7df77bf21 100755 --- a/kernel_lite/ipc_posix/signal/SignalTest.cpp +++ b/kernel_lite/ipc_posix/signal/SignalTest.cpp @@ -262,7 +262,10 @@ HWTEST_F(IpcSignalTest, testAbortHandler, Function | MediumTest | Level1) { const int memSize = 32; mShmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT); - ASSERT_NE(mShmid, -1) << "> parent: get share mem if fail, errno = " << errno; + if (mShmid == -1){ + LOG("shmget errno = %d\n", errno); + ADD_FAILURE(); + } pid_t pid = fork(); ASSERT_TRUE(pid >= 0) << "======== Fork Error! ========="; diff --git a/kernel_lite/net_posix/src/ActsNetTest.cpp b/kernel_lite/net_posix/src/ActsNetTest.cpp index d8f0bfb6d..7ed18c837 100755 --- a/kernel_lite/net_posix/src/ActsNetTest.cpp +++ b/kernel_lite/net_posix/src/ActsNetTest.cpp @@ -356,18 +356,24 @@ static void* SampleTcpClientTask(void *p) return nullptr; } -static void* TcpServerLoopTask(void *p) +static void *TcpServerLoopTask(void *p) { int srvFd = CommInitTcpServer(STACK_PORT); EXPECT_NE(-1, srvFd); + if (srvFd == -1) + { + LOG("CommInitTcpServer errno = %d\n", errno); + ADD_FAILURE(); + } int i = 0; int clientFds[36]; struct sockaddr_in clnAddr = {0}; socklen_t clnAddrLen = sizeof(clnAddr); - while (i < 30) { - clientFds[i] = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen); + while (i < 30) + { + clientFds[i] = accept(srvFd, (struct sockaddr *)&clnAddr, &clnAddrLen); printf("[***---][tcp server loop]accept <%s:%d>, fd[%d]i[%d]\n", inet_ntoa(clnAddr.sin_addr), - ntohs(clnAddr.sin_port), clientFds[i], i); + ntohs(clnAddr.sin_port), clientFds[i], i); EXPECT_NE(-1, clientFds[i]); i++; } diff --git a/kernel_lite/time_posix/src/TimeUtilsTest.cpp b/kernel_lite/time_posix/src/TimeUtilsTest.cpp index 67a683cf7..0f77c8b67 100755 --- a/kernel_lite/time_posix/src/TimeUtilsTest.cpp +++ b/kernel_lite/time_posix/src/TimeUtilsTest.cpp @@ -253,7 +253,10 @@ HWTEST_F(TimeUtilsTest, testStrftime, Function | MediumTest | Level3) char buffer[80] = {0}; time_t mtime = 18880; struct tm *localTime = localtime(&mtime); - ASSERT_NE(nullptr, localTime); + if (localTime == nullptr) { + LOG("localtime errno "); + ADD_FAILURE(); + } size_t ftime = strftime(buffer, sizeof(buffer) - 1, "%Ex %EX %A", localTime); EXPECT_GT(ftime, g_zero) << "strftime return error!"; EXPECT_STREQ(buffer, "01/01/70 05:14:40 Thursday") << "buffer return error!"; diff --git a/kernel_lite/utils/mt_utils.cpp b/kernel_lite/utils/mt_utils.cpp index 39e1995cb..6953024ee 100755 --- a/kernel_lite/utils/mt_utils.cpp +++ b/kernel_lite/utils/mt_utils.cpp @@ -35,16 +35,20 @@ uint64_t CheckStep(int value) shmctl(g_shmidCheckStep, IPC_RMID, nullptr); g_shmidCheckStep = shmget(IPC_PRIVATE, 1024, 0666 | IPC_CREAT); } - uint64_t *shared = (uint64_t *)shmat(g_shmidCheckStep, nullptr, 0); + if (g_shmidCheckStep != -1) { - if (value == 1) { - *shared = 1; - } else { - *shared = (*shared << 4) + value; + uint64_t *shared = (uint64_t *)shmat(g_shmidCheckStep, nullptr, 0); + + if (value == 1) { + *shared = 1; + } else { + *shared = (*shared << 4) + value; + } + uint64_t state = *shared; + shmdt(shared); + + return state; } - uint64_t state = *shared; - shmdt(shared); - return state; } int CountPrimes(uint32_t maxNumber) -- GitLab