Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory/alloc: Use SCCalloc instead of malloc/memset #9858

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/alert-debuglog.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,9 @@ static TmEcode AlertDebugLogDecoderEvent(ThreadVars *tv, const Packet *p, void *

static TmEcode AlertDebugLogThreadInit(ThreadVars *t, const void *initdata, void **data)
{
AlertDebugLogThread *aft = SCMalloc(sizeof(AlertDebugLogThread));
AlertDebugLogThread *aft = SCCalloc(1, sizeof(AlertDebugLogThread));
if (unlikely(aft == NULL))
return TM_ECODE_FAILED;
memset(aft, 0, sizeof(AlertDebugLogThread));

if(initdata == NULL)
{
Expand Down Expand Up @@ -447,11 +446,10 @@ static OutputInitResult AlertDebugLogInitCtx(ConfNode *conf)
goto error;
}

OutputCtx *output_ctx = SCMalloc(sizeof(OutputCtx));
OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
if (unlikely(output_ctx == NULL))
goto error;

memset(output_ctx, 0x00, sizeof(OutputCtx));
output_ctx->data = file_ctx;
output_ctx->DeInit = AlertDebugLogDeInitCtx;

Expand Down
3 changes: 1 addition & 2 deletions src/alert-fastlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,9 @@ int AlertFastLogger(ThreadVars *tv, void *data, const Packet *p)

TmEcode AlertFastLogThreadInit(ThreadVars *t, const void *initdata, void **data)
{
AlertFastLogThread *aft = SCMalloc(sizeof(AlertFastLogThread));
AlertFastLogThread *aft = SCCalloc(1, sizeof(AlertFastLogThread));
if (unlikely(aft == NULL))
return TM_ECODE_FAILED;
memset(aft, 0, sizeof(AlertFastLogThread));
if(initdata == NULL)
{
SCLogDebug("Error getting context for AlertFastLog. \"initdata\" argument NULL");
Expand Down
7 changes: 2 additions & 5 deletions src/alert-syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,12 @@ static OutputInitResult AlertSyslogInitCtx(ConfNode *conf)

openlog(ident, LOG_PID|LOG_NDELAY, facility);

OutputCtx *output_ctx = SCMalloc(sizeof(OutputCtx));
OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
if (unlikely(output_ctx == NULL)) {
SCLogDebug("could not create new OutputCtx");
LogFileFreeCtx(logfile_ctx);
return result;
}
memset(output_ctx, 0x00, sizeof(OutputCtx));

output_ctx->data = logfile_ctx;
output_ctx->DeInit = AlertSyslogDeInitCtx;
Expand Down Expand Up @@ -155,12 +154,10 @@ static TmEcode AlertSyslogThreadInit(ThreadVars *t, const void *initdata, void *
return TM_ECODE_FAILED;
}

AlertSyslogThread *ast = SCMalloc(sizeof(AlertSyslogThread));
AlertSyslogThread *ast = SCCalloc(1, sizeof(AlertSyslogThread));
if (unlikely(ast == NULL))
return TM_ECODE_FAILED;

memset(ast, 0, sizeof(AlertSyslogThread));

/** Use the Output Context (file pointer and mutex) */
ast->file_ctx = ((OutputCtx *)initdata)->data;

Expand Down
17 changes: 7 additions & 10 deletions src/app-layer-detect-proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,11 @@ static AppLayerProtoDetectProbingParserElement *AppLayerProtoDetectProbingParser
{
SCEnter();

AppLayerProtoDetectProbingParserElement *p = SCMalloc(sizeof(AppLayerProtoDetectProbingParserElement));
AppLayerProtoDetectProbingParserElement *p =
SCCalloc(1, sizeof(AppLayerProtoDetectProbingParserElement));
if (unlikely(p == NULL)) {
exit(EXIT_FAILURE);
}
memset(p, 0, sizeof(AppLayerProtoDetectProbingParserElement));

SCReturnPtr(p, "AppLayerProtoDetectProbingParserElement");
}
Expand All @@ -714,11 +714,11 @@ static AppLayerProtoDetectProbingParserPort *AppLayerProtoDetectProbingParserPor
{
SCEnter();

AppLayerProtoDetectProbingParserPort *p = SCMalloc(sizeof(AppLayerProtoDetectProbingParserPort));
AppLayerProtoDetectProbingParserPort *p =
SCCalloc(1, sizeof(AppLayerProtoDetectProbingParserPort));
if (unlikely(p == NULL)) {
exit(EXIT_FAILURE);
}
memset(p, 0, sizeof(AppLayerProtoDetectProbingParserPort));

SCReturnPtr(p, "AppLayerProtoDetectProbingParserPort");
}
Expand Down Expand Up @@ -752,11 +752,10 @@ static AppLayerProtoDetectProbingParser *AppLayerProtoDetectProbingParserAlloc(v
{
SCEnter();

AppLayerProtoDetectProbingParser *p = SCMalloc(sizeof(AppLayerProtoDetectProbingParser));
AppLayerProtoDetectProbingParser *p = SCCalloc(1, sizeof(AppLayerProtoDetectProbingParser));
if (unlikely(p == NULL)) {
exit(EXIT_FAILURE);
}
memset(p, 0, sizeof(AppLayerProtoDetectProbingParser));

SCReturnPtr(p, "AppLayerProtoDetectProbingParser");
}
Expand Down Expand Up @@ -1269,10 +1268,9 @@ static int AppLayerProtoDetectPMMapSignatures(AppLayerProtoDetectPMCtx *ctx)
int mpm_ret;
SigIntId id = 0;

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

/* add an array indexed by rule id to look up the sig */
for (s = ctx->head; s != NULL; ) {
Expand Down Expand Up @@ -1985,10 +1983,9 @@ AppLayerProtoDetectThreadCtx *AppLayerProtoDetectGetCtxThread(void)
}
}

alpd_tctx = SCMalloc(sizeof(*alpd_tctx));
alpd_tctx = SCCalloc(1, sizeof(*alpd_tctx));
if (alpd_tctx == NULL)
goto error;
memset(alpd_tctx, 0, sizeof(*alpd_tctx));

/* Get the max pat id for all the mpm ctxs. */
if (PmqSetup(&alpd_tctx->pmq) < 0)
Expand Down
2 changes: 0 additions & 2 deletions src/app-layer-enip-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ static CIPServiceEntry *CIPServiceAlloc(ENIPTransaction *tx)
if (unlikely(svc == NULL))
return NULL;

memset(svc, 0x00, sizeof(CIPServiceEntry));

TAILQ_INIT(&svc->segment_list);
TAILQ_INIT(&svc->attrib_list);

Expand Down
5 changes: 1 addition & 4 deletions src/app-layer-enip.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,10 @@ static int ENIPStateGetEventInfoById(int event_id, const char **event_name,
static void *ENIPStateAlloc(void *orig_state, AppProto proto_orig)
{
SCLogDebug("ENIPStateAlloc");
void *s = SCMalloc(sizeof(ENIPState));
void *s = SCCalloc(1, sizeof(ENIPState));
if (unlikely(s == NULL))
return NULL;

memset(s, 0, sizeof(ENIPState));

ENIPState *enip_state = (ENIPState *) s;

TAILQ_INIT(&enip_state->tx_list);
Expand Down Expand Up @@ -242,7 +240,6 @@ static ENIPTransaction *ENIPTransactionAlloc(ENIPState *state)
state->curr = tx;
state->transaction_max++;

memset(tx, 0x00, sizeof(ENIPTransaction));
TAILQ_INIT(&tx->service_list);

tx->enip = state;
Expand Down
3 changes: 1 addition & 2 deletions src/app-layer-events.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ int AppLayerGetPktEventInfo(const char *event_name, int *event_id)
void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event)
{
if (*sevents == NULL) {
AppLayerDecoderEvents *new_devents = SCMalloc(sizeof(AppLayerDecoderEvents));
AppLayerDecoderEvents *new_devents = SCCalloc(1, sizeof(AppLayerDecoderEvents));
if (new_devents == NULL)
return;

memset(new_devents, 0, sizeof(AppLayerDecoderEvents));
*sevents = new_devents;

}
Expand Down
3 changes: 1 addition & 2 deletions src/app-layer-ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1216,11 +1216,10 @@ static AppLayerGetFileState FTPDataStateGetTxFiles(void *_state, void *tx, uint8

static void FTPSetMpmState(void)
{
ftp_mpm_ctx = SCMalloc(sizeof(MpmCtx));
ftp_mpm_ctx = SCCalloc(1, sizeof(MpmCtx));
if (unlikely(ftp_mpm_ctx == NULL)) {
exit(EXIT_FAILURE);
}
memset(ftp_mpm_ctx, 0, sizeof(MpmCtx));
MpmInitCtx(ftp_mpm_ctx, FTP_MPM);

uint32_t i = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/app-layer-htp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3011,10 +3011,9 @@ void HTPConfigure(void)
SCLogDebug("LIBHTP server %s", s->name);

HTPCfgRec *nextrec = cfglist.next;
HTPCfgRec *htprec = SCMalloc(sizeof(HTPCfgRec));
HTPCfgRec *htprec = SCCalloc(1, sizeof(HTPCfgRec));
if (NULL == htprec)
exit(EXIT_FAILURE);
memset(htprec, 0x00, sizeof(*htprec));

cfglist.next = htprec;

Expand Down
3 changes: 1 addition & 2 deletions src/app-layer-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,9 @@ AppLayerParserState *AppLayerParserStateAlloc(void)
{
SCEnter();

AppLayerParserState *pstate = (AppLayerParserState *)SCMalloc(sizeof(*pstate));
AppLayerParserState *pstate = (AppLayerParserState *)SCCalloc(1, sizeof(*pstate));
if (pstate == NULL)
goto end;
memset(pstate, 0, sizeof(*pstate));

end:
SCReturnPtr(pstate, "AppLayerParserState");
Expand Down
9 changes: 3 additions & 6 deletions src/app-layer-smtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1521,10 +1521,9 @@ static AppLayerResult SMTPParseServerRecord(Flow *f, void *alstate, AppLayerPars
*/
void *SMTPStateAlloc(void *orig_state, AppProto proto_orig)
{
SMTPState *smtp_state = SCMalloc(sizeof(SMTPState));
SMTPState *smtp_state = SCCalloc(1, sizeof(SMTPState));
if (unlikely(smtp_state == NULL))
return NULL;
memset(smtp_state, 0, sizeof(SMTPState));

smtp_state->cmds = SCMalloc(sizeof(uint8_t) *
SMTP_COMMAND_BUFFER_STEPS);
Expand All @@ -1541,10 +1540,9 @@ void *SMTPStateAlloc(void *orig_state, AppProto proto_orig)

static SMTPString *SMTPStringAlloc(void)
{
SMTPString *smtp_string = SCMalloc(sizeof(SMTPString));
SMTPString *smtp_string = SCCalloc(1, sizeof(SMTPString));
if (unlikely(smtp_string == NULL))
return NULL;
memset(smtp_string, 0, sizeof(SMTPString));

return smtp_string;
}
Expand Down Expand Up @@ -1656,11 +1654,10 @@ static void SMTPStateFree(void *p)

static void SMTPSetMpmState(void)
{
smtp_mpm_ctx = SCMalloc(sizeof(MpmCtx));
smtp_mpm_ctx = SCCalloc(1, sizeof(MpmCtx));
if (unlikely(smtp_mpm_ctx == NULL)) {
exit(EXIT_FAILURE);
}
memset(smtp_mpm_ctx, 0, sizeof(MpmCtx));
MpmInitCtx(smtp_mpm_ctx, SMTP_MPM);

uint32_t i = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/app-layer-ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2646,10 +2646,9 @@ static AppLayerResult SSLParseServerRecord(Flow *f, void *alstate, AppLayerParse
*/
static void *SSLStateAlloc(void *orig_state, AppProto proto_orig)
{
SSLState *ssl_state = SCMalloc(sizeof(SSLState));
SSLState *ssl_state = SCCalloc(1, sizeof(SSLState));
if (unlikely(ssl_state == NULL))
return NULL;
memset(ssl_state, 0, sizeof(SSLState));
ssl_state->client_connp.cert_log_flag = 0;
ssl_state->server_connp.cert_log_flag = 0;
memset(ssl_state->client_connp.random, 0, TLS_RANDOM_LEN);
Expand Down
3 changes: 1 addition & 2 deletions src/app-layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,10 +992,9 @@ AppLayerThreadCtx *AppLayerGetCtxThread(ThreadVars *tv)
{
SCEnter();

AppLayerThreadCtx *app_tctx = SCMalloc(sizeof(*app_tctx));
AppLayerThreadCtx *app_tctx = SCCalloc(1, sizeof(*app_tctx));
if (app_tctx == NULL)
goto error;
memset(app_tctx, 0, sizeof(*app_tctx));

if ((app_tctx->alpd_tctx = AppLayerProtoDetectGetCtxThread()) == NULL)
goto error;
Expand Down
3 changes: 1 addition & 2 deletions src/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,8 @@ DecodeThreadVars *DecodeThreadVarsAlloc(ThreadVars *tv)
{
DecodeThreadVars *dtv = NULL;

if ( (dtv = SCMalloc(sizeof(DecodeThreadVars))) == NULL)
if ((dtv = SCCalloc(1, sizeof(DecodeThreadVars))) == NULL)
return NULL;
memset(dtv, 0, sizeof(DecodeThreadVars));

dtv->app_tctx = AppLayerGetCtxThread(tv);

Expand Down
4 changes: 1 addition & 3 deletions src/defrag-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@ static DefragTracker *DefragTrackerAlloc(void)

(void) SC_ATOMIC_ADD(defrag_memuse, sizeof(DefragTracker));

DefragTracker *dt = SCMalloc(sizeof(DefragTracker));
DefragTracker *dt = SCCalloc(1, sizeof(DefragTracker));
if (unlikely(dt == NULL))
goto error;

memset(dt, 0x00, sizeof(DefragTracker));

SCMutexInit(&dt->lock, NULL);
SC_ATOMIC_INIT(dt->use_cnt);
return dt;
Expand Down
3 changes: 1 addition & 2 deletions src/detect-byte-extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,9 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
goto error;
}

bed = SCMalloc(sizeof(DetectByteExtractData));
bed = SCCalloc(1, sizeof(DetectByteExtractData));
if (unlikely(bed == NULL))
goto error;
memset(bed, 0, sizeof(DetectByteExtractData));

/* no of bytes to extract */
char nbytes_str[64] = "";
Expand Down
21 changes: 7 additions & 14 deletions src/detect-csum.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,8 @@ static int DetectIPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char

//printf("DetectCsumSetup: \'%s\'\n", csum_str);

if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL)
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
memset(cd, 0, sizeof(DetectCsumData));

if (DetectCsumParseArg(csum_str, cd) == 0)
goto error;
Expand Down Expand Up @@ -368,9 +367,8 @@ static int DetectTCPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const cha

//printf("DetectCsumSetup: \'%s\'\n", csum_str);

if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL)
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
memset(cd, 0, sizeof(DetectCsumData));

if (DetectCsumParseArg(csum_str, cd) == 0)
goto error;
Expand Down Expand Up @@ -459,9 +457,8 @@ static int DetectTCPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const cha

//printf("DetectCsumSetup: \'%s\'\n", csum_str);

if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL)
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
memset(cd, 0, sizeof(DetectCsumData));

if (DetectCsumParseArg(csum_str, cd) == 0)
goto error;
Expand Down Expand Up @@ -550,9 +547,8 @@ static int DetectUDPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const cha

//printf("DetectCsumSetup: \'%s\'\n", csum_str);

if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL)
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
memset(cd, 0, sizeof(DetectCsumData));

if (DetectCsumParseArg(csum_str, cd) == 0)
goto error;
Expand Down Expand Up @@ -641,9 +637,8 @@ static int DetectUDPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const cha

//printf("DetectCsumSetup: \'%s\'\n", csum_str);

if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL)
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
memset(cd, 0, sizeof(DetectCsumData));

if (DetectCsumParseArg(csum_str, cd) == 0)
goto error;
Expand Down Expand Up @@ -730,9 +725,8 @@ static int DetectICMPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const ch

//printf("DetectCsumSetup: \'%s\'\n", csum_str);

if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL)
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
memset(cd, 0, sizeof(DetectCsumData));

if (DetectCsumParseArg(csum_str, cd) == 0)
goto error;
Expand Down Expand Up @@ -822,9 +816,8 @@ static int DetectICMPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const ch
{
DetectCsumData *cd = NULL;

if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL)
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
memset(cd, 0, sizeof(DetectCsumData));

if (DetectCsumParseArg(csum_str, cd) == 0)
goto error;
Expand Down
Loading
Loading