提交 d75563fb 编写于 作者: C cheng_jinsong

malloc/calloc分配内存后检验

Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
上级 35c8fb3f
......@@ -37,6 +37,7 @@ static int main_cmd(BShellHandle shell, int argc, char **argv)
}
size_t serviceNameLen = strlen(argv[1]) + strlen(argv[2]) + 2; // 2 is \0 and #
char *cmd = (char *)calloc(1, serviceNameLen);
BEGET_ERROR_CHECK(cmd != NULL, return 0, "failed to allocate cmd memory");
BEGET_ERROR_CHECK(sprintf_s(cmd, serviceNameLen, "%s#%s", argv[1], argv[2]) >= 0, free(cmd);
return 0, "dump service arg create failed");
CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_DUMP, cmd);
......
......@@ -82,15 +82,18 @@ HWTEST_F(ServiceUnitTest, case02, TestSize.Level1)
cJSON *serviceItem = cJSON_GetObjectItem(jobItem, "services");
ASSERT_NE(nullptr, serviceItem);
Service *service = AddService("test_service8");
ASSERT_NE(nullptr, service);
int ret = ParseOneService(serviceItem, service);
EXPECT_EQ(ret, 0);
int *fds = (int *)malloc(sizeof(int) * 1); // ServiceStop will release fds
ASSERT_NE(nullptr, fds);
UpdaterServiceFds(service, fds, 1);
service->attribute = SERVICE_ATTR_ONDEMAND;
ret = ServiceStart(service);
EXPECT_EQ(ret, 0);
CmdLines *cmdline = (CmdLines *)malloc(sizeof(CmdLines) + sizeof(CmdLine));
ASSERT_NE(nullptr, cmdline);
cmdline->cmdNum = 1;
cmdline->cmds[0].cmdIndex = 0;
service->restartArg = cmdline;
......@@ -213,20 +216,26 @@ HWTEST_F(ServiceUnitTest, TestServiceManagerRelease, TestSize.Level1)
service = AddService("test_service5");
ASSERT_NE(nullptr, service);
service->pathArgs.argv = (char **)malloc(sizeof(char *));
ASSERT_NE(nullptr, service->pathArgs.argv);
service->pathArgs.count = 1;
const char *path = "/data/init_ut/test_service_release";
service->pathArgs.argv[0] = strdup(path);
service->writePidArgs.argv = (char **)malloc(sizeof(char *));
ASSERT_NE(nullptr, service->writePidArgs.argv);
service->writePidArgs.count = 1;
service->writePidArgs.argv[0] = strdup(path);
service->servPerm.caps = (unsigned int *)malloc(sizeof(unsigned int));
ASSERT_NE(nullptr, service->servPerm.caps);
service->servPerm.gIDArray = (gid_t *)malloc(sizeof(gid_t));
ASSERT_NE(nullptr, service->servPerm.gIDArray);
service->socketCfg = (ServiceSocket *)malloc(sizeof(ServiceSocket));
ASSERT_NE(nullptr, service->socketCfg);
service->socketCfg->sockFd = 0;
service->socketCfg->next = nullptr;
service->fileCfg = (ServiceFile *)malloc(sizeof(ServiceFile));
ASSERT_NE(nullptr, service->fileCfg);
service->fileCfg->fd = 0;
service->fileCfg->next = nullptr;
ReleaseService(service);
......@@ -330,6 +339,7 @@ HWTEST_F(ServiceUnitTest, TestServiceExec, TestSize.Level1)
service->servPerm.gIDCnt = -1;
service->servPerm.uID = 0;
unsigned int *caps = (unsigned int *)calloc(1, sizeof(unsigned int) * 1);
ASSERT_NE(nullptr, caps);
caps[0] = FULL_CAP;
service->servPerm.caps = caps;
service->servPerm.capsCnt = 1;
......
......@@ -405,7 +405,9 @@ HWTEST_F(ParamUnitTest, TestLinuxRWLock, TestSize.Level0)
ParamRWMutexDelete(nullptr);
ParamMutexDelete(nullptr);
WorkSpace *workspace1 = (WorkSpace *)malloc(sizeof(WorkSpace) + strlen("testfilename1"));
ASSERT_NE(nullptr, workspace1);
WorkSpace *workspace2 = (WorkSpace *)malloc(sizeof(WorkSpace) + strlen("testfilename1"));
ASSERT_NE(nullptr, workspace2);
if (strcpy_s(workspace1->fileName, strlen("testfilename1"), "testfilename") != EOK) {
EXPECT_EQ(0, 1);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册