From: Eric Leblond Date: Fri, 21 Sep 2012 13:24:17 +0000 (+0200) Subject: Add some missing checks of SCMalloc return. X-Git-Tag: suricata-1.4beta2~39 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=655577cbbc2000e0dc6dbe3d6248bedc7a797d07;p=thirdparty%2Fsuricata.git Add some missing checks of SCMalloc return. --- diff --git a/src/detect-fileext.c b/src/detect-fileext.c index 6d724b3429..520f60881c 100644 --- a/src/detect-fileext.c +++ b/src/detect-fileext.c @@ -156,9 +156,11 @@ static DetectFileextData *DetectFileextParse (char *str) #ifdef DEBUG if (SCLogDebugEnabled()) { char *ext = SCMalloc(fileext->len + 1); - memcpy(ext, fileext->ext, fileext->len); - ext[fileext->len] = '\0'; - SCLogDebug("will look for fileext %s", ext); + if (ext != NULL) { + memcpy(ext, fileext->ext, fileext->len); + ext[fileext->len] = '\0'; + SCLogDebug("will look for fileext %s", ext); + } } #endif diff --git a/src/detect-filemagic.c b/src/detect-filemagic.c index 725a148e2b..ed2e78fa53 100644 --- a/src/detect-filemagic.c +++ b/src/detect-filemagic.c @@ -167,9 +167,11 @@ static int DetectFilemagicMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, #ifdef DEBUG if (SCLogDebugEnabled()) { char *name = SCMalloc(filemagic->len + 1); - memcpy(name, filemagic->name, filemagic->len); - name[filemagic->len] = '\0'; - SCLogDebug("will look for filemagic %s", name); + if (name != NULL) { + memcpy(name, filemagic->name, filemagic->len); + name[filemagic->len] = '\0'; + SCLogDebug("will look for filemagic %s", name); + } } #endif @@ -222,9 +224,11 @@ static DetectFilemagicData *DetectFilemagicParse (char *str) #ifdef DEBUG if (SCLogDebugEnabled()) { char *name = SCMalloc(filemagic->len + 1); - memcpy(name, filemagic->name, filemagic->len); - name[filemagic->len] = '\0'; - SCLogDebug("will look for filemagic %s", name); + if (name != NULL) { + memcpy(name, filemagic->name, filemagic->len); + name[filemagic->len] = '\0'; + SCLogDebug("will look for filemagic %s", name); + } } #endif diff --git a/src/detect-filename.c b/src/detect-filename.c index 16275388ec..2940f17c01 100644 --- a/src/detect-filename.c +++ b/src/detect-filename.c @@ -109,9 +109,11 @@ static int DetectFilenameMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, #ifdef DEBUG if (SCLogDebugEnabled()) { char *name = SCMalloc(filename->len + 1); - memcpy(name, filename->name, filename->len); - name[filename->len] = '\0'; - SCLogDebug("will look for filename %s", name); + if (name != NULL) { + memcpy(name, filename->name, filename->len); + name[filename->len] = '\0'; + SCLogDebug("will look for filename %s", name); + } } #endif @@ -165,9 +167,11 @@ static DetectFilenameData *DetectFilenameParse (char *str) #ifdef DEBUG if (SCLogDebugEnabled()) { char *name = SCMalloc(filename->len + 1); - memcpy(name, filename->name, filename->len); - name[filename->len] = '\0'; - SCLogDebug("will look for filename %s", name); + if (name != NULL) { + memcpy(name, filename->name, filename->len); + name[filename->len] = '\0'; + SCLogDebug("will look for filename %s", name); + } } #endif diff --git a/src/util-cuda-handlers.c b/src/util-cuda-handlers.c index 987692c232..e340357384 100644 --- a/src/util-cuda-handlers.c +++ b/src/util-cuda-handlers.c @@ -470,6 +470,9 @@ int SCCudaHlGetCudaModule(CUmodule *p_module, const char *ptx_image, int handle) /* select the ptx image based on the compute capability supported by all * devices (i.e. the lowest) */ char* image = SCMalloc(strlen(ptx_image)+15); + if (image == NULL) { + exit(EXIT_FAILURE); + } memset(image, 0x0, sizeof(image)); int major = INT_MAX; diff --git a/src/util-pool.c b/src/util-pool.c index 108d66f6f8..581824f9d2 100644 --- a/src/util-pool.c +++ b/src/util-pool.c @@ -116,9 +116,9 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void * uint32_t u32 = 0; if (size > 0) { PoolBucket *pb = SCCalloc(size, sizeof(PoolBucket)); - p->pb_buffer = pb; - if (pb == NULL) + if (unlikely(pb == NULL)) goto error; + p->pb_buffer = pb; memset(pb, 0, size * sizeof(PoolBucket)); for (u32 = 0; u32 < size; u32++) { /* populate pool */ @@ -350,6 +350,8 @@ void PoolPrintSaturation(Pool *p) { void *PoolTestAlloc() { void *ptr = SCMalloc(10); + if (ptr == NULL) + return NULL; return ptr; } int PoolTestInitArg(void *data, void *allocdata) { diff --git a/src/util-profiling-rules.c b/src/util-profiling-rules.c index fffbf5df09..086db4af87 100644 --- a/src/util-profiling-rules.c +++ b/src/util-profiling-rules.c @@ -168,6 +168,10 @@ void SCProfilingRulesGlobalInit(void) { log_dir = DEFAULT_LOG_DIR; profiling_file_name = SCMalloc(PATH_MAX); + if (profiling_file_name == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "can't duplicate file name"); + exit(EXIT_FAILURE); + } snprintf(profiling_file_name, PATH_MAX, "%s/%s", log_dir, filename); const char *v = ConfNodeLookupChildValue(conf, "append"); diff --git a/src/util-profiling.c b/src/util-profiling.c index 4eee57deab..ee54a5dd2f 100644 --- a/src/util-profiling.c +++ b/src/util-profiling.c @@ -141,6 +141,11 @@ SCProfilingInit(void) log_dir = DEFAULT_LOG_DIR; profiling_packets_file_name = SCMalloc(PATH_MAX); + if (profiling_packets_file_name == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "can't duplicate file name"); + exit(EXIT_FAILURE); + } + snprintf(profiling_packets_file_name, PATH_MAX, "%s/%s", log_dir, filename); const char *v = ConfNodeLookupChildValue(conf, "append"); @@ -215,6 +220,11 @@ SCProfilingInit(void) log_dir = DEFAULT_LOG_DIR; profiling_locks_file_name = SCMalloc(PATH_MAX); + if (profiling_locks_file_name == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "can't duplicate file name"); + exit(EXIT_FAILURE); + } + snprintf(profiling_locks_file_name, PATH_MAX, "%s/%s", log_dir, filename); const char *v = ConfNodeLookupChildValue(conf, "append"); diff --git a/src/win32-misc.c b/src/win32-misc.c index 09d3130d3b..83f86aa8b5 100644 --- a/src/win32-misc.c +++ b/src/win32-misc.c @@ -33,6 +33,8 @@ void setenv(const char *name, const char *value, int overwrite) { if (overwrite || NULL == getenv(name)) { char *str = SCMalloc(strlen(name) + strlen(value) + 2); + if (str == NULL) + return; snprintf(str, strlen(name) + strlen(value) + 1, "%s=%s", name, value); putenv(str); SCFree(str); @@ -42,6 +44,8 @@ void setenv(const char *name, const char *value, int overwrite) void unsetenv(const char *name) { char *str = SCMalloc(strlen(name) + 2); + if (str == NULL) + return; snprintf(str, strlen(name) + 1, "%s=", name); putenv(str); SCFree(str);