From: Eero Tamminen Date: Wed, 8 Jun 2022 09:31:07 +0000 (+0300) Subject: Add scalloc() and use it in src/processes.c (#4014) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dfd034032b7c7c8f821774715c0723c42cefd332;p=thirdparty%2Fcollectd.git Add scalloc() and use it in src/processes.c (#4014) * Add scalloc() wrapper similar to smalloc() to common utils scalloc() wraps calloc() with exit on alloc failure, similarly to what smalloc() does for malloc(). * Handle (Solaris-only) ps_read_process calloc fails by using scalloc Everything else checks and handles calloc failures except this function. As I cannot test Solaris specific code, I've just replaced calloc with scalloc, which gracefully exits collectd with error message on alloc failures (instead of corrupting memory / crashing, as would happen with current code). --- diff --git a/src/processes.c b/src/processes.c index 54da9b356..c36a9ca83 100644 --- a/src/processes.c +++ b/src/processes.c @@ -1658,15 +1658,15 @@ static int ps_read_process(long pid, process_entry_t *ps, char *state) { snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid); snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid); - buffer = calloc(1, sizeof(pstatus_t)); + buffer = scalloc(1, sizeof(pstatus_t)); read_file_contents(filename, buffer, sizeof(pstatus_t)); myStatus = (pstatus_t *)buffer; - buffer = calloc(1, sizeof(psinfo_t)); + buffer = scalloc(1, sizeof(psinfo_t)); read_file_contents(f_psinfo, buffer, sizeof(psinfo_t)); myInfo = (psinfo_t *)buffer; - buffer = calloc(1, sizeof(prusage_t)); + buffer = scalloc(1, sizeof(prusage_t)); read_file_contents(f_usage, buffer, sizeof(prusage_t)); myUsage = (prusage_t *)buffer; diff --git a/src/utils/common/common.c b/src/utils/common/common.c index 2a205537b..d15444614 100644 --- a/src/utils/common/common.c +++ b/src/utils/common/common.c @@ -205,7 +205,7 @@ char *sstrerror(int errnum, char *buf, size_t buflen) { pthread_mutex_unlock(&strerror_r_lock); } - /* #endif !HAVE_STRERROR_R */ + /* #endif !HAVE_STRERROR_R */ #elif STRERROR_R_CHAR_P { @@ -221,7 +221,7 @@ char *sstrerror(int errnum, char *buf, size_t buflen) { buflen); } } - /* #endif STRERROR_R_CHAR_P */ + /* #endif STRERROR_R_CHAR_P */ #else if (strerror_r(errnum, buf, buflen) != 0) { @@ -235,6 +235,17 @@ char *sstrerror(int errnum, char *buf, size_t buflen) { return buf; } /* char *sstrerror */ +void *scalloc(size_t nmemb, size_t size) { + void *r; + + if ((r = calloc(nmemb, size)) == NULL) { + ERROR("Not enough memory."); + exit(3); + } + + return r; +} /* void *scalloc */ + void *smalloc(size_t size) { void *r; @@ -793,8 +804,8 @@ unsigned long long htonll(unsigned long long n) { #endif /* HAVE_HTONLL */ #if FP_LAYOUT_NEED_NOTHING - /* Well, we need nothing.. */ - /* #endif FP_LAYOUT_NEED_NOTHING */ +/* Well, we need nothing.. */ +/* #endif FP_LAYOUT_NEED_NOTHING */ #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP #if FP_LAYOUT_NEED_ENDIANFLIP diff --git a/src/utils/common/common.h b/src/utils/common/common.h index fce2d12bb..feae1aba8 100644 --- a/src/utils/common/common.h +++ b/src/utils/common/common.h @@ -75,6 +75,7 @@ __attribute__((format(printf, 1, 2))) char *ssnprintf_alloc(char const *format, char *sstrdup(const char *s); size_t sstrnlen(const char *s, size_t n); char *sstrndup(const char *s, size_t n); +void *scalloc(size_t nmemb, size_t size); void *smalloc(size_t size); char *sstrerror(int errnum, char *buf, size_t buflen);