detect/csum: general code cleanups

pull/11326/head
Victor Julien 2 years ago committed by Victor Julien
parent 956c8bebd1
commit 64f5865efc

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2022 Open Information Security Foundation /* Copyright (C) 2007-2024 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -196,13 +196,13 @@ static int DetectCsumParseArg(const char *key, DetectCsumData *cd)
if (key[0] == '\"' && key[strlen(key) - 1] == '\"') { if (key[0] == '\"' && key[strlen(key) - 1] == '\"') {
str = SCStrdup(key + 1); str = SCStrdup(key + 1);
if (unlikely(str == NULL)) { if (unlikely(str == NULL)) {
goto error; return 0;
} }
str[strlen(key) - 2] = '\0'; str[strlen(key) - 2] = '\0';
} else { } else {
str = SCStrdup(key); str = SCStrdup(key);
if (unlikely(str == NULL)) { if (unlikely(str == NULL)) {
goto error; return 0;
} }
} }
@ -213,9 +213,7 @@ static int DetectCsumParseArg(const char *key, DetectCsumData *cd)
return 1; return 1;
} }
error: SCFree(str);
if (str != NULL)
SCFree(str);
return 0; return 0;
} }
@ -274,12 +272,9 @@ static int DetectIPV4CsumMatch(DetectEngineThreadCtx *det_ctx,
*/ */
static int DetectIPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str) static int DetectIPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str)
{ {
DetectCsumData *cd = NULL; DetectCsumData *cd = SCCalloc(1, sizeof(DetectCsumData));
if (cd == NULL)
//printf("DetectCsumSetup: \'%s\'\n", csum_str); return -1;
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
if (DetectCsumParseArg(csum_str, cd) == 0) if (DetectCsumParseArg(csum_str, cd) == 0)
goto error; goto error;
@ -292,18 +287,13 @@ static int DetectIPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char
return 0; return 0;
error: error:
if (cd != NULL) DetectIPV4CsumFree(de_ctx, cd);
DetectIPV4CsumFree(de_ctx, cd);
return -1; return -1;
} }
static void DetectIPV4CsumFree(DetectEngineCtx *de_ctx, void *ptr) static void DetectIPV4CsumFree(DetectEngineCtx *de_ctx, void *ptr)
{ {
DetectCsumData *cd = (DetectCsumData *)ptr; SCFree(ptr);
if (cd != NULL)
SCFree(cd);
} }
/** /**
@ -362,12 +352,9 @@ static int DetectTCPV4CsumMatch(DetectEngineThreadCtx *det_ctx,
*/ */
static int DetectTCPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str) static int DetectTCPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str)
{ {
DetectCsumData *cd = NULL; DetectCsumData *cd = SCCalloc(1, sizeof(DetectCsumData));
if (cd == NULL)
//printf("DetectCsumSetup: \'%s\'\n", csum_str); return -1;
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
if (DetectCsumParseArg(csum_str, cd) == 0) if (DetectCsumParseArg(csum_str, cd) == 0)
goto error; goto error;
@ -380,18 +367,13 @@ static int DetectTCPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const cha
return 0; return 0;
error: error:
if (cd != NULL) DetectTCPV4CsumFree(de_ctx, cd);
DetectTCPV4CsumFree(de_ctx, cd);
return -1; return -1;
} }
static void DetectTCPV4CsumFree(DetectEngineCtx *de_ctx, void *ptr) static void DetectTCPV4CsumFree(DetectEngineCtx *de_ctx, void *ptr)
{ {
DetectCsumData *cd = (DetectCsumData *)ptr; SCFree(ptr);
if (cd != NULL)
SCFree(cd);
} }
/** /**
@ -451,12 +433,9 @@ static int DetectTCPV6CsumMatch(DetectEngineThreadCtx *det_ctx,
*/ */
static int DetectTCPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str) static int DetectTCPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str)
{ {
DetectCsumData *cd = NULL; DetectCsumData *cd = SCCalloc(1, sizeof(DetectCsumData));
if (cd == NULL)
//printf("DetectCsumSetup: \'%s\'\n", csum_str); return -1;
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
if (DetectCsumParseArg(csum_str, cd) == 0) if (DetectCsumParseArg(csum_str, cd) == 0)
goto error; goto error;
@ -469,18 +448,13 @@ static int DetectTCPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const cha
return 0; return 0;
error: error:
if (cd != NULL) DetectTCPV6CsumFree(de_ctx, cd);
DetectTCPV6CsumFree(de_ctx, cd);
return -1; return -1;
} }
static void DetectTCPV6CsumFree(DetectEngineCtx *de_ctx, void *ptr) static void DetectTCPV6CsumFree(DetectEngineCtx *de_ctx, void *ptr)
{ {
DetectCsumData *cd = (DetectCsumData *)ptr; SCFree(ptr);
if (cd != NULL)
SCFree(cd);
} }
/** /**
@ -542,12 +516,9 @@ static int DetectUDPV4CsumMatch(DetectEngineThreadCtx *det_ctx,
*/ */
static int DetectUDPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str) static int DetectUDPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str)
{ {
DetectCsumData *cd = NULL; DetectCsumData *cd = SCCalloc(1, sizeof(DetectCsumData));
if (cd == NULL)
//printf("DetectCsumSetup: \'%s\'\n", csum_str); return -1;
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
if (DetectCsumParseArg(csum_str, cd) == 0) if (DetectCsumParseArg(csum_str, cd) == 0)
goto error; goto error;
@ -560,18 +531,13 @@ static int DetectUDPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const cha
return 0; return 0;
error: error:
if (cd != NULL) DetectUDPV4CsumFree(de_ctx, cd);
DetectUDPV4CsumFree(de_ctx, cd);
return -1; return -1;
} }
static void DetectUDPV4CsumFree(DetectEngineCtx *de_ctx, void *ptr) static void DetectUDPV4CsumFree(DetectEngineCtx *de_ctx, void *ptr)
{ {
DetectCsumData *cd = (DetectCsumData *)ptr; SCFree(ptr);
if (cd != NULL)
SCFree(cd);
} }
/** /**
@ -630,12 +596,9 @@ static int DetectUDPV6CsumMatch(DetectEngineThreadCtx *det_ctx,
*/ */
static int DetectUDPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str) static int DetectUDPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str)
{ {
DetectCsumData *cd = NULL; DetectCsumData *cd = SCCalloc(1, sizeof(DetectCsumData));
if (cd == NULL)
//printf("DetectCsumSetup: \'%s\'\n", csum_str); return -1;
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
if (DetectCsumParseArg(csum_str, cd) == 0) if (DetectCsumParseArg(csum_str, cd) == 0)
goto error; goto error;
@ -648,9 +611,7 @@ static int DetectUDPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const cha
return 0; return 0;
error: error:
if (cd != NULL) DetectUDPV6CsumFree(de_ctx, cd);
DetectUDPV6CsumFree(de_ctx, cd);
return -1; return -1;
} }
@ -718,12 +679,9 @@ static int DetectICMPV4CsumMatch(DetectEngineThreadCtx *det_ctx,
*/ */
static int DetectICMPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str) static int DetectICMPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str)
{ {
DetectCsumData *cd = NULL; DetectCsumData *cd = SCCalloc(1, sizeof(DetectCsumData));
if (cd == NULL)
//printf("DetectCsumSetup: \'%s\'\n", csum_str); return -1;
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL)
goto error;
if (DetectCsumParseArg(csum_str, cd) == 0) if (DetectCsumParseArg(csum_str, cd) == 0)
goto error; goto error;
@ -736,18 +694,13 @@ static int DetectICMPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const ch
return 0; return 0;
error: error:
if (cd != NULL) DetectICMPV4CsumFree(de_ctx, cd);
DetectICMPV4CsumFree(de_ctx, cd);
return -1; return -1;
} }
static void DetectICMPV4CsumFree(DetectEngineCtx *de_ctx, void *ptr) static void DetectICMPV4CsumFree(DetectEngineCtx *de_ctx, void *ptr)
{ {
DetectCsumData *cd = (DetectCsumData *)ptr; SCFree(ptr);
if (cd != NULL)
SCFree(cd);
} }
/** /**
@ -813,10 +766,9 @@ static int DetectICMPV6CsumMatch(DetectEngineThreadCtx *det_ctx,
*/ */
static int DetectICMPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str) static int DetectICMPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const char *csum_str)
{ {
DetectCsumData *cd = NULL; DetectCsumData *cd = SCCalloc(1, sizeof(DetectCsumData));
if (cd == NULL)
if ((cd = SCCalloc(1, sizeof(DetectCsumData))) == NULL) return -1;
goto error;
if (DetectCsumParseArg(csum_str, cd) == 0) if (DetectCsumParseArg(csum_str, cd) == 0)
goto error; goto error;
@ -829,18 +781,13 @@ static int DetectICMPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, const ch
return 0; return 0;
error: error:
if (cd != NULL) DetectICMPV6CsumFree(de_ctx, cd);
DetectICMPV6CsumFree(de_ctx, cd);
return -1; return -1;
} }
static void DetectICMPV6CsumFree(DetectEngineCtx *de_ctx, void *ptr) static void DetectICMPV6CsumFree(DetectEngineCtx *de_ctx, void *ptr)
{ {
DetectCsumData *cd = (DetectCsumData *)ptr; SCFree(ptr);
if (cd != NULL)
SCFree(cd);
} }
/* ---------------------------------- Unit Tests --------------------------- */ /* ---------------------------------- Unit Tests --------------------------- */

Loading…
Cancel
Save