unit: check return value of malloc

This commit is contained in:
Wang, Xiang W 2019-01-29 04:45:51 -05:00 committed by Chang, Harry
parent eda1871f86
commit b0c0d9cd92
2 changed files with 9 additions and 0 deletions

View File

@ -477,6 +477,7 @@ TEST(HybridArgChecks, AllocScratchBogusScratch) {
makeDatabase(&db); makeDatabase(&db);
ch_scratch_t *blah = (ch_scratch_t *)malloc(100); ch_scratch_t *blah = (ch_scratch_t *)malloc(100);
ASSERT_TRUE(blah != nullptr);
memset(blah, 0xf0, 100); memset(blah, 0xf0, 100);
ch_error_t err = ch_alloc_scratch(db, &blah); ch_error_t err = ch_alloc_scratch(db, &blah);
ASSERT_EQ(CH_INVALID, err); ASSERT_EQ(CH_INVALID, err);
@ -536,6 +537,7 @@ TEST(HybridArgChecks, DatabaseSizeNoDatabase) {
TEST(HybridArgChecks, CloneBadScratch) { TEST(HybridArgChecks, CloneBadScratch) {
// Try cloning the scratch // Try cloning the scratch
void *local_garbage = malloc(sizeof(garbage)); void *local_garbage = malloc(sizeof(garbage));
ASSERT_TRUE(local_garbage != nullptr);
memcpy(local_garbage, garbage, sizeof(garbage)); memcpy(local_garbage, garbage, sizeof(garbage));
ch_scratch_t *cloned = nullptr; ch_scratch_t *cloned = nullptr;
ch_scratch_t *scratch = (ch_scratch_t *)local_garbage; ch_scratch_t *scratch = (ch_scratch_t *)local_garbage;
@ -550,6 +552,7 @@ TEST(HybridArgChecks, ScanBadScratch) {
makeDatabase(&db); makeDatabase(&db);
void *local_garbage = malloc(sizeof(garbage)); void *local_garbage = malloc(sizeof(garbage));
ASSERT_TRUE(local_garbage != nullptr);
memcpy(local_garbage, garbage, sizeof(garbage)); memcpy(local_garbage, garbage, sizeof(garbage));
ch_scratch_t *scratch = (ch_scratch_t *)local_garbage; ch_scratch_t *scratch = (ch_scratch_t *)local_garbage;

View File

@ -1370,6 +1370,7 @@ TEST(HyperscanArgChecks, AllocScratchBogusScratch) {
ASSERT_EQ(HS_SUCCESS, err); ASSERT_EQ(HS_SUCCESS, err);
ASSERT_TRUE(db != nullptr); ASSERT_TRUE(db != nullptr);
hs_scratch_t *blah = (hs_scratch_t *)malloc(100); hs_scratch_t *blah = (hs_scratch_t *)malloc(100);
ASSERT_TRUE(blah != nullptr);
memset(blah, 0xf0, 100); memset(blah, 0xf0, 100);
err = hs_alloc_scratch(db, &blah); err = hs_alloc_scratch(db, &blah);
ASSERT_EQ(HS_INVALID, err); ASSERT_EQ(HS_INVALID, err);
@ -2034,6 +2035,7 @@ TEST(HyperscanArgChecks, ScratchSizeBadScratch) {
TEST(HyperscanArgChecks, CloneBadScratch) { TEST(HyperscanArgChecks, CloneBadScratch) {
// Try cloning the scratch // Try cloning the scratch
void *local_garbage = malloc(sizeof(garbage)); void *local_garbage = malloc(sizeof(garbage));
ASSERT_TRUE(local_garbage != nullptr);
memcpy(local_garbage, garbage, sizeof(garbage)); memcpy(local_garbage, garbage, sizeof(garbage));
hs_scratch_t *cloned = nullptr; hs_scratch_t *cloned = nullptr;
hs_scratch_t *scratch = (hs_scratch_t *)local_garbage; hs_scratch_t *scratch = (hs_scratch_t *)local_garbage;
@ -2052,6 +2054,7 @@ TEST(HyperscanArgChecks, ScanBadScratch) {
ASSERT_TRUE(db != nullptr); ASSERT_TRUE(db != nullptr);
void *local_garbage = malloc(sizeof(garbage)); void *local_garbage = malloc(sizeof(garbage));
ASSERT_TRUE(local_garbage != nullptr);
memcpy(local_garbage, garbage, sizeof(garbage)); memcpy(local_garbage, garbage, sizeof(garbage));
hs_scratch_t *scratch = (hs_scratch_t *)local_garbage; hs_scratch_t *scratch = (hs_scratch_t *)local_garbage;
@ -2072,6 +2075,7 @@ TEST(HyperscanArgChecks, ScanStreamBadScratch) {
ASSERT_EQ(HS_SUCCESS, err); ASSERT_EQ(HS_SUCCESS, err);
ASSERT_TRUE(db != nullptr); ASSERT_TRUE(db != nullptr);
void *local_garbage = malloc(sizeof(garbage)); void *local_garbage = malloc(sizeof(garbage));
ASSERT_TRUE(local_garbage != nullptr);
memcpy(local_garbage, garbage, sizeof(garbage)); memcpy(local_garbage, garbage, sizeof(garbage));
hs_scratch_t *scratch = (hs_scratch_t *)local_garbage; hs_scratch_t *scratch = (hs_scratch_t *)local_garbage;
@ -2107,6 +2111,7 @@ TEST(HyperscanArgChecks, ResetStreamBadScratch) {
ASSERT_EQ(HS_SUCCESS, err); ASSERT_EQ(HS_SUCCESS, err);
ASSERT_TRUE(db != nullptr); ASSERT_TRUE(db != nullptr);
void *local_garbage = malloc(sizeof(garbage)); void *local_garbage = malloc(sizeof(garbage));
ASSERT_TRUE(local_garbage != nullptr);
memcpy(local_garbage, garbage, sizeof(garbage)); memcpy(local_garbage, garbage, sizeof(garbage));
hs_scratch_t *scratch = (hs_scratch_t *)local_garbage; hs_scratch_t *scratch = (hs_scratch_t *)local_garbage;
@ -2142,6 +2147,7 @@ TEST(HyperscanArgChecks, ScanVectorBadScratch) {
ASSERT_EQ(HS_SUCCESS, err); ASSERT_EQ(HS_SUCCESS, err);
ASSERT_TRUE(db != nullptr); ASSERT_TRUE(db != nullptr);
void *local_garbage = malloc(sizeof(garbage)); void *local_garbage = malloc(sizeof(garbage));
ASSERT_TRUE(local_garbage != nullptr);
memcpy(local_garbage, garbage, sizeof(garbage)); memcpy(local_garbage, garbage, sizeof(garbage));
hs_scratch_t *scratch = (hs_scratch_t *)local_garbage; hs_scratch_t *scratch = (hs_scratch_t *)local_garbage;