Skip to content

Commit

Permalink
calloc: Use nmemb with SCCalloc
Browse files Browse the repository at this point in the history
This commit modifies calls to SCCalloc that had a member count of 1 and
a size count calculated as: element_count * sizeof(element).
  • Loading branch information
jlucovsky committed Nov 21, 2023
1 parent 193e0ea commit ec1482c
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/app-layer-detect-proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ static int AppLayerProtoDetectPMMapSignatures(AppLayerProtoDetectPMCtx *ctx)
int mpm_ret;
SigIntId id = 0;

ctx->map = SCCalloc(1, ctx->max_sig_id * sizeof(AppLayerProtoDetectPMSignature *));
ctx->map = SCCalloc(ctx->max_sig_id, sizeof(AppLayerProtoDetectPMSignature *));
if (ctx->map == NULL)
goto error;

Expand Down
2 changes: 1 addition & 1 deletion src/detect-engine-build.c
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ int SigAddressPrepareStage1(DetectEngineCtx *de_ctx)

de_ctx->sig_array_len = DetectEngineGetMaxSigId(de_ctx);
de_ctx->sig_array_size = (de_ctx->sig_array_len * sizeof(Signature *));
de_ctx->sig_array = (Signature **)SCCalloc(1, de_ctx->sig_array_size);
de_ctx->sig_array = (Signature **)SCCalloc(de_ctx->sig_array_len, sizeof(Signature *));
if (de_ctx->sig_array == NULL)
goto error;

Expand Down
6 changes: 3 additions & 3 deletions src/detect-engine-siggroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ int SigGroupHeadBuildMatchArray(DetectEngineCtx *de_ctx, SigGroupHead *sgh,

BUG_ON(sgh->init->match_array != NULL);

sgh->init->match_array = SCCalloc(1, sgh->init->sig_cnt * sizeof(Signature *));
sgh->init->match_array = SCCalloc(sgh->init->sig_cnt, sizeof(Signature *));
if (sgh->init->match_array == NULL)
return -1;

Expand Down Expand Up @@ -672,12 +672,12 @@ int SigGroupHeadBuildNonPrefilterArray(DetectEngineCtx *de_ctx, SigGroupHead *sg
}

if (non_pf > 0) {
sgh->non_pf_other_store_array = SCCalloc(1, non_pf * sizeof(SignatureNonPrefilterStore));
sgh->non_pf_other_store_array = SCCalloc(non_pf, sizeof(SignatureNonPrefilterStore));
BUG_ON(sgh->non_pf_other_store_array == NULL);
}

if (non_pf_syn > 0) {
sgh->non_pf_syn_store_array = SCCalloc(1, non_pf_syn * sizeof(SignatureNonPrefilterStore));
sgh->non_pf_syn_store_array = SCCalloc(non_pf_syn, sizeof(SignatureNonPrefilterStore));
BUG_ON(sgh->non_pf_syn_store_array == NULL);
}

Expand Down
4 changes: 2 additions & 2 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -3046,7 +3046,7 @@ static int DetectEngineThreadCtxInitKeywords(DetectEngineCtx *de_ctx, DetectEngi
{
if (de_ctx->keyword_id > 0) {
// coverity[suspicious_sizeof : FALSE]
det_ctx->keyword_ctxs_array = SCCalloc(1, de_ctx->keyword_id * sizeof(void *));
det_ctx->keyword_ctxs_array = SCCalloc(de_ctx->keyword_id, sizeof(void *));
if (det_ctx->keyword_ctxs_array == NULL) {
SCLogError("setting up thread local detect ctx");
return TM_ECODE_FAILED;
Expand Down Expand Up @@ -3226,7 +3226,7 @@ static TmEcode ThreadCtxDoInit (DetectEngineCtx *de_ctx, DetectEngineThreadCtx *
/* DeState */
if (de_ctx->sig_array_len > 0) {
det_ctx->match_array_len = de_ctx->sig_array_len;
det_ctx->match_array = SCCalloc(1, det_ctx->match_array_len * sizeof(Signature *));
det_ctx->match_array = SCCalloc(det_ctx->match_array_len, sizeof(Signature *));
if (det_ctx->match_array == NULL) {
return TM_ECODE_FAILED;
}
Expand Down
2 changes: 1 addition & 1 deletion src/util-bloomfilter-counting.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ BloomFilterCounting *BloomFilterCountingInit(uint32_t size, uint8_t type, uint8_
bf->Hash = Hash;

/* setup the bitarray */
bf->array = SCCalloc(1, bf->array_size * bf->type);
bf->array = SCCalloc(bf->array_size, bf->type);
if (bf->array == NULL)
goto error;

Expand Down
2 changes: 1 addition & 1 deletion src/util-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ HashTable* HashTableInit(uint32_t size, uint32_t (*Hash)(struct HashTable_ *, vo
ht->Compare = HashTableDefaultCompare;

/* setup the bitarray */
ht->array = SCCalloc(1, ht->array_size * sizeof(HashTableBucket *));
ht->array = SCCalloc(ht->array_size, sizeof(HashTableBucket *));
if (ht->array == NULL)
goto error;

Expand Down
2 changes: 1 addition & 1 deletion src/util-hashlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ HashListTable *HashListTableInit(uint32_t size,
ht->Compare = HashListTableDefaultCompare;

/* setup the bitarray */
ht->array = SCCalloc(1, ht->array_size * sizeof(HashListTableBucket *));
ht->array = SCCalloc(ht->array_size, sizeof(HashListTableBucket *));
if (ht->array == NULL) {
sc_errno = SC_ENOMEM;
goto error;
Expand Down
12 changes: 6 additions & 6 deletions src/util-mpm-ac-bs.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ static inline void SCACBSCreateFailureTable(MpmCtx *mpm_ctx)

/* allot space for the failure table. A failure entry in the table for
* every state(SCACBSCtx->state_count) */
ctx->failure_table = SCCalloc(1, ctx->state_count * sizeof(int32_t));
ctx->failure_table = SCCalloc(ctx->state_count, sizeof(int32_t));
if (ctx->failure_table == NULL) {
FatalError("Error allocating memory");
}
Expand Down Expand Up @@ -681,7 +681,7 @@ static inline void SCACBSCreateModDeltaTable(MpmCtx *mpm_ctx)

/* buffer to hold pointers in the buffer, so that a state can use it
* directly to access its state data */
ctx->state_table_mod_pointers = SCCalloc(1, ctx->state_count * sizeof(uint8_t *));
ctx->state_table_mod_pointers = SCCalloc(ctx->state_count, sizeof(uint8_t *));
if (ctx->state_table_mod_pointers == NULL) {
FatalError("Error allocating memory");
}
Expand Down Expand Up @@ -750,7 +750,7 @@ static inline void SCACBSCreateModDeltaTable(MpmCtx *mpm_ctx)

/* buffer to hold pointers in the buffer, so that a state can use it
* directly to access its state data */
ctx->state_table_mod_pointers = SCCalloc(1, ctx->state_count * sizeof(uint8_t *));
ctx->state_table_mod_pointers = SCCalloc(ctx->state_count, sizeof(uint8_t *));
if (ctx->state_table_mod_pointers == NULL) {
FatalError("Error allocating memory");
}
Expand Down Expand Up @@ -861,7 +861,7 @@ int SCACBSPreparePatterns(MpmCtx *mpm_ctx)
}

/* alloc the pattern array */
ctx->parray = (MpmPattern **)SCCalloc(1, mpm_ctx->pattern_cnt * sizeof(MpmPattern *));
ctx->parray = (MpmPattern **)SCCalloc(mpm_ctx->pattern_cnt, sizeof(MpmPattern *));
if (ctx->parray == NULL)
goto error;
mpm_ctx->memory_cnt++;
Expand All @@ -887,7 +887,7 @@ int SCACBSPreparePatterns(MpmCtx *mpm_ctx)
ctx->single_state_size = sizeof(int32_t) * 256;

/* handle no case patterns */
ctx->pid_pat_list = SCCalloc(1, (mpm_ctx->max_pat_id + 1) * sizeof(SCACBSPatternList));
ctx->pid_pat_list = SCCalloc((mpm_ctx->max_pat_id + 1), sizeof(SCACBSPatternList));
if (ctx->pid_pat_list == NULL) {
FatalError("Error allocating memory");
}
Expand Down Expand Up @@ -949,7 +949,7 @@ void SCACBSInitCtx(MpmCtx *mpm_ctx)
mpm_ctx->memory_size += sizeof(SCACBSCtx);

/* initialize the hash we use to speed up pattern insertions */
mpm_ctx->init_hash = SCCalloc(1, sizeof(MpmPattern *) * MPM_INIT_HASH_SIZE);
mpm_ctx->init_hash = SCCalloc(MPM_INIT_HASH_SIZE, sizeof(MpmPattern *));
if (mpm_ctx->init_hash == NULL) {
exit(EXIT_FAILURE);
}
Expand Down
6 changes: 3 additions & 3 deletions src/util-mpm-ac-ks.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ static void SCACTileCreateFailureTable(MpmCtx *mpm_ctx)

/* Allocate space for the failure table. A failure entry in the table for
* every state(SCACTileCtx->state_count) */
ctx->failure_table = SCCalloc(1, ctx->state_count * sizeof(int32_t));
ctx->failure_table = SCCalloc(ctx->state_count, sizeof(int32_t));
if (ctx->failure_table == NULL) {
FatalError("Error allocating memory");
}
Expand Down Expand Up @@ -874,7 +874,7 @@ int SCACTilePreparePatterns(MpmCtx *mpm_ctx)
}

/* alloc the pattern array */
ctx->parray = (MpmPattern **)SCCalloc(1, mpm_ctx->pattern_cnt * sizeof(MpmPattern *));
ctx->parray = (MpmPattern **)SCCalloc(mpm_ctx->pattern_cnt, sizeof(MpmPattern *));
if (ctx->parray == NULL)
goto error;

Expand Down Expand Up @@ -985,7 +985,7 @@ void SCACTileInitCtx(MpmCtx *mpm_ctx)
mpm_ctx->memory_size += sizeof(SCACTileCtx);

/* initialize the hash we use to speed up pattern insertions */
mpm_ctx->init_hash = SCCalloc(1, sizeof(MpmPattern *) * MPM_INIT_HASH_SIZE);
mpm_ctx->init_hash = SCCalloc(MPM_INIT_HASH_SIZE, sizeof(MpmPattern *));
if (mpm_ctx->init_hash == NULL) {
exit(EXIT_FAILURE);
}
Expand Down
8 changes: 4 additions & 4 deletions src/util-mpm-ac.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ static inline void SCACCreateFailureTable(MpmCtx *mpm_ctx)

/* allot space for the failure table. A failure entry in the table for
* every state(SCACCtx->state_count) */
ctx->failure_table = SCCalloc(1, ctx->state_count * sizeof(int32_t));
ctx->failure_table = SCCalloc(ctx->state_count, sizeof(int32_t));
if (ctx->failure_table == NULL) {
FatalError("Error allocating memory");
}
Expand Down Expand Up @@ -736,7 +736,7 @@ int SCACPreparePatterns(MpmCtx *mpm_ctx)
}

/* alloc the pattern array */
ctx->parray = (MpmPattern **)SCCalloc(1, mpm_ctx->pattern_cnt * sizeof(MpmPattern *));
ctx->parray = (MpmPattern **)SCCalloc(mpm_ctx->pattern_cnt, sizeof(MpmPattern *));
if (ctx->parray == NULL)
goto error;
mpm_ctx->memory_cnt++;
Expand All @@ -762,7 +762,7 @@ int SCACPreparePatterns(MpmCtx *mpm_ctx)
ctx->single_state_size = sizeof(int32_t) * 256;

/* handle no case patterns */
ctx->pid_pat_list = SCCalloc(1, (mpm_ctx->max_pat_id + 1) * sizeof(SCACPatternList));
ctx->pid_pat_list = SCCalloc((mpm_ctx->max_pat_id + 1), sizeof(SCACPatternList));
if (ctx->pid_pat_list == NULL) {
FatalError("Error allocating memory");
}
Expand Down Expand Up @@ -831,7 +831,7 @@ void SCACInitCtx(MpmCtx *mpm_ctx)
mpm_ctx->memory_size += sizeof(SCACCtx);

/* initialize the hash we use to speed up pattern insertions */
mpm_ctx->init_hash = SCCalloc(1, sizeof(MpmPattern *) * MPM_INIT_HASH_SIZE);
mpm_ctx->init_hash = SCCalloc(MPM_INIT_HASH_SIZE, sizeof(MpmPattern *));
if (mpm_ctx->init_hash == NULL) {
exit(EXIT_FAILURE);
}
Expand Down
14 changes: 7 additions & 7 deletions src/util-mpm-hs.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,29 +379,29 @@ typedef struct SCHSCompileData_ {

static SCHSCompileData *SCHSAllocCompileData(unsigned int pattern_cnt)
{
SCHSCompileData *cd = SCCalloc(1, pattern_cnt * sizeof(SCHSCompileData));
SCHSCompileData *cd = SCCalloc(pattern_cnt, sizeof(SCHSCompileData));
if (cd == NULL) {
goto error;
}

cd->pattern_cnt = pattern_cnt;

cd->ids = SCCalloc(1, pattern_cnt * sizeof(unsigned int));
cd->ids = SCCalloc(pattern_cnt, sizeof(unsigned int));
if (cd->ids == NULL) {
goto error;
}

cd->flags = SCCalloc(1, pattern_cnt * sizeof(unsigned int));
cd->flags = SCCalloc(pattern_cnt, sizeof(unsigned int));
if (cd->flags == NULL) {
goto error;
}

cd->expressions = SCCalloc(1, pattern_cnt * sizeof(char *));
cd->expressions = SCCalloc(pattern_cnt, sizeof(char *));
if (cd->expressions == NULL) {
goto error;
}

cd->ext = SCCalloc(1, pattern_cnt * sizeof(hs_expr_ext_t *));
cd->ext = SCCalloc(pattern_cnt, sizeof(hs_expr_ext_t *));
if (cd->ext == NULL) {
goto error;
}
Expand Down Expand Up @@ -559,7 +559,7 @@ static PatternDatabase *PatternDatabaseAlloc(uint32_t pattern_cnt)
pd->hs_db = NULL;

/* alloc the pattern array */
pd->parray = (SCHSPattern **)SCCalloc(1, pd->pattern_cnt * sizeof(SCHSPattern *));
pd->parray = (SCHSPattern **)SCCalloc(pd->pattern_cnt, sizeof(SCHSPattern *));
if (pd->parray == NULL) {
SCFree(pd);
return NULL;
Expand Down Expand Up @@ -806,7 +806,7 @@ void SCHSInitCtx(MpmCtx *mpm_ctx)

/* initialize the hash we use to speed up pattern insertions */
SCHSCtx *ctx = (SCHSCtx *)mpm_ctx->ctx;
ctx->init_hash = SCCalloc(1, sizeof(SCHSPattern *) * INIT_HASH_SIZE);
ctx->init_hash = SCCalloc(INIT_HASH_SIZE, sizeof(SCHSPattern *));
if (ctx->init_hash == NULL) {
exit(EXIT_FAILURE);
}
Expand Down
8 changes: 4 additions & 4 deletions src/util-profiling-keywords.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void SCProfilingKeywordThreadSetup(SCProfileKeywordDetectCtx *ctx, DetectEngineT
if (ctx == NULL)
return;

SCProfileKeywordData *a = SCCalloc(1, sizeof(SCProfileKeywordData) * DETECT_TBLSIZE);
SCProfileKeywordData *a = SCCalloc(DETECT_TBLSIZE, sizeof(SCProfileKeywordData));
if (a != NULL) {
det_ctx->keyword_perf_data = a;
}
Expand All @@ -294,7 +294,7 @@ void SCProfilingKeywordThreadSetup(SCProfileKeywordDetectCtx *ctx, DetectEngineT

int i;
for (i = 0; i < nlists; i++) {
SCProfileKeywordData *b = SCCalloc(1, sizeof(SCProfileKeywordData) * DETECT_TBLSIZE);
SCProfileKeywordData *b = SCCalloc(DETECT_TBLSIZE, sizeof(SCProfileKeywordData));
if (b != NULL) {
det_ctx->keyword_perf_data_per_list[i] = b;
}
Expand Down Expand Up @@ -369,7 +369,7 @@ SCProfilingKeywordInitCounters(DetectEngineCtx *de_ctx)
de_ctx->profile_keyword_ctx = SCProfilingKeywordInitCtx();
BUG_ON(de_ctx->profile_keyword_ctx == NULL);

de_ctx->profile_keyword_ctx->data = SCCalloc(1, sizeof(SCProfileKeywordData) * DETECT_TBLSIZE);
de_ctx->profile_keyword_ctx->data = SCCalloc(DETECT_TBLSIZE, sizeof(SCProfileKeywordData));
BUG_ON(de_ctx->profile_keyword_ctx->data == NULL);

de_ctx->profile_keyword_ctx_per_list = SCCalloc(nlists, sizeof(SCProfileKeywordDetectCtx *));
Expand All @@ -380,7 +380,7 @@ SCProfilingKeywordInitCounters(DetectEngineCtx *de_ctx)
de_ctx->profile_keyword_ctx_per_list[i] = SCProfilingKeywordInitCtx();
BUG_ON(de_ctx->profile_keyword_ctx_per_list[i] == NULL);
de_ctx->profile_keyword_ctx_per_list[i]->data =
SCCalloc(1, sizeof(SCProfileKeywordData) * DETECT_TBLSIZE);
SCCalloc(DETECT_TBLSIZE, sizeof(SCProfileKeywordData));
BUG_ON(de_ctx->profile_keyword_ctx_per_list[i]->data == NULL);
}

Expand Down
4 changes: 2 additions & 2 deletions src/util-profiling-rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ void SCProfilingRuleThreadSetup(SCProfileDetectCtx *ctx, DetectEngineThreadCtx *
if (ctx == NULL|| ctx->size == 0)
return;

SCProfileData *a = SCCalloc(1, sizeof(SCProfileData) * ctx->size);
SCProfileData *a = SCCalloc(ctx->size, sizeof(SCProfileData));
if (a != NULL) {
det_ctx->rule_perf_data = a;
det_ctx->rule_perf_data_size = ctx->size;
Expand Down Expand Up @@ -665,7 +665,7 @@ SCProfilingRuleInitCounters(DetectEngineCtx *de_ctx)
}

if (count > 0) {
de_ctx->profile_ctx->data = SCCalloc(1, sizeof(SCProfileData) * de_ctx->profile_ctx->size);
de_ctx->profile_ctx->data = SCCalloc(de_ctx->profile_ctx->size, sizeof(SCProfileData));
BUG_ON(de_ctx->profile_ctx->data == NULL);

sig = de_ctx->sig_list;
Expand Down
6 changes: 3 additions & 3 deletions src/util-storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ int StorageFinalize(void)
if (count == 0)
return 0;

storage_map = SCCalloc(1, sizeof(StorageMapping *) * STORAGE_MAX);
storage_map = SCCalloc(STORAGE_MAX, sizeof(StorageMapping *));
if (unlikely(storage_map == NULL)) {
return -1;
}

for (i = 0; i < STORAGE_MAX; i++) {
if (storage_max_id[i] > 0) {
storage_map[i] = SCCalloc(1, sizeof(StorageMapping) * storage_max_id[i]);
storage_map[i] = SCCalloc(storage_max_id[i], sizeof(StorageMapping));
if (storage_map[i] == NULL)
return -1;
}
Expand Down Expand Up @@ -262,7 +262,7 @@ void *StorageAllocById(Storage **storage, StorageEnum type, int id)
Storage *store = *storage;
if (store == NULL) {
// coverity[suspicious_sizeof : FALSE]
store = SCCalloc(1, sizeof(void *) * storage_max_id[type]);
store = SCCalloc(storage_max_id[type], sizeof(void *));
if (unlikely(store == NULL))
return NULL;
}
Expand Down

0 comments on commit ec1482c

Please sign in to comment.