]> granicus.if.org Git - procps-ng/commitdiff
top: end reliance on strdup not failing & roll our own
authorJim Warner <james.warner@comcast.net>
Mon, 11 Feb 2013 06:00:00 +0000 (00:00 -0600)
committerCraig Small <csmall@enc.com.au>
Sat, 23 Feb 2013 21:43:44 +0000 (08:43 +1100)
Lately, top has begun to rely more and more on dynamic
memory allocations rather than the static buffers that
were found in many of its structures. This was perhaps
most evident in the increasing use of the strdup call.

This commit trades that function call for the internal
equivalent which will protect us from malloc failures.

(everything is perfectly justified plus right margins)
(are completely filled, but of course it must be luck)

Signed-off-by: Jim Warner <james.warner@comcast.net>
top/top.c
top/top.h

index 39dbee0f11d8dd3dcc696d7cf75087e5925961e5..a21cf7701ea61d70e16551340b2b6bc74dea41b4 100644 (file)
--- a/top/top.c
+++ b/top/top.c
@@ -890,6 +890,12 @@ static void *alloc_r (void *ptr, size_t num) {
 } // end: alloc_r
 
 
+static char *alloc_s (const char *str) MALLOC;
+static char *alloc_s (const char *str) {
+   return strcpy(alloc_c(strlen(str) +1), str);
+} // end: alloc_s
+
+
         /*
          * This function is used in connection with raw single byte
          * unsolicited keyboard input that's susceptible to SIGWINCH
@@ -3207,7 +3213,7 @@ static void configs_read (void) {
 try_inspect_entries:
       // we'll start off Inspect stuff with 1 'potential' blank line
       // ( only realized if we end up with Inspect.total > 0 )
-      for (i = 0, Inspect.raw = strdup("\n");;) {
+      for (i = 0, Inspect.raw = alloc_s("\n");;) {
        #define iT(element) Inspect.tab[i].element
          size_t lraw = strlen(Inspect.raw) +1;
          char *s;
@@ -3222,11 +3228,11 @@ try_inspect_entries:
          p = fmtmk(N_fmt(YINSP_rcfile_fmt), i +1);
 
          if (!(s = strtok(fbuf, "\t\n"))) { Rc_questions = 1; continue; }
-         iT(type) = strdup(s);
+         iT(type) = alloc_s(s);
          if (!(s = strtok(NULL, "\t\n"))) { Rc_questions = 1; continue; }
-         iT(name) = strdup(s);
+         iT(name) = alloc_s(s);
          if (!(s = strtok(NULL, "\t\n"))) { Rc_questions = 1; continue; }
-         iT(fmts) = strdup(s);
+         iT(fmts) = alloc_s(s);
 
          switch (toupper(fbuf[0])) {
             case 'F':
@@ -3257,10 +3263,10 @@ try_inspect_entries:
          Inspect.total = Inspect.demo = MAXTBL(sels);
          Inspect.tab = alloc_c(sizeof(struct I_ent) * Inspect.total);
          for (i = 0; i < Inspect.total; i++) {
-            Inspect.tab[i].type = strdup(N_txt(YINSP_deqtyp_txt));
-            Inspect.tab[i].name = strdup(sels[i]);
+            Inspect.tab[i].type = alloc_s(N_txt(YINSP_deqtyp_txt));
+            Inspect.tab[i].name = alloc_s(sels[i]);
             Inspect.tab[i].func = insp_do_demo;
-            Inspect.tab[i].fmts = strdup(N_txt(YINSP_deqfmt_txt));
+            Inspect.tab[i].fmts = alloc_s(N_txt(YINSP_deqfmt_txt));
             Inspect.tab[i].fstr = alloc_c(FNDBUFSIZ);
          }
        #undef mkS
index 1e0f89dc67c374c8913819da2f96e1e7694a8d68..b6b0446966b958539c8df88e193059747f4863d2 100644 (file)
--- a/top/top.h
+++ b/top/top.h
@@ -631,6 +631,7 @@ typedef struct WIN_t {
 /*------  Low Level Memory/Keyboard/File I/O support  --------------------*/
 //atic void         *alloc_c (size_t num);
 //atic void         *alloc_r (void *ptr, size_t num);
+//atic char         *alloc_s (const char *str);
 //atic inline int    ioa (struct timespec *ts);
 //atic int           ioch (int ech, char *buf, unsigned cnt);
 //atic int           iokey (int init);