]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Add scalloc() and use it in src/processes.c (#4014)
authorEero Tamminen <eero.t.tamminen@intel.com>
Wed, 8 Jun 2022 09:31:07 +0000 (12:31 +0300)
committerGitHub <noreply@github.com>
Wed, 8 Jun 2022 09:31:07 +0000 (11:31 +0200)
* 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).

src/processes.c
src/utils/common/common.c
src/utils/common/common.h

index 54da9b3561b9eb6b49fee7afcf66111d405867e7..c36a9ca83b8cc3c8c2c1d68b24256fe866a293d0 100644 (file)
@@ -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;
 
index 2a205537b31cc45959f3bbe373b1d490d5e87be2..d154446141422621dd6ee65bfa31a9780a41db00 100644 (file)
@@ -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
index fce2d12bbbc611e2e0683055b297869d05faf3c3..feae1aba86da3e84d63c3eaeeed2f962b097125a 100644 (file)
@@ -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);