]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: seq,who: prefer xasprintf
authorCollin Funk <collin.funk1@gmail.com>
Sat, 14 Feb 2026 22:48:45 +0000 (14:48 -0800)
committerCollin Funk <collin.funk1@gmail.com>
Sat, 14 Feb 2026 22:48:45 +0000 (14:48 -0800)
* src/seq.c (main): Use xasprintf instead of asprintf followed by a
check if the allocation fails.
* src/who.c (print_line): Likewise.

src/seq.c
src/who.c

index 1a3a40eb6227f46842cef44f00fe4c3012d1e180..29c332da019f147e63dcf23d09a1a959cae49197 100644 (file)
--- a/src/seq.c
+++ b/src/seq.c
@@ -27,6 +27,7 @@
 #include "full-write.h"
 #include "quote.h"
 #include "xstrtod.h"
+#include "xvasprintf.h"
 
 /* Roll our own isfinite/isnan rather than using <math.h>, so that we don't
    have to worry about linking -lm just for isfinite.  */
@@ -683,16 +684,11 @@ main (int argc, char **argv)
       && 0 < step.value && step.value <= SEQ_FAST_STEP_LIMIT
       && !equal_width && !format_str && strlen (separator) == 1)
     {
-      char *s1;
-      char *s2;
-      if (all_digits_p (user_start))
-        s1 = xstrdup (user_start);
-      else if (asprintf (&s1, "%0.Lf", first.value) < 0)
-        xalloc_die ();
-      if (! isfinite (last.value))
-        s2 = xstrdup ("inf"); /* Ensure "inf" is used.  */
-      else if (asprintf (&s2, "%0.Lf", last.value) < 0)
-        xalloc_die ();
+      char *s1 = (all_digits_p (user_start)
+                  ? xstrdup (user_start) : xasprintf ("%0.Lf", first.value));
+      /* Ensure "inf" is used.  */
+      char *s2 = (! isfinite (last.value)
+                  ? xstrdup ("inf") : xasprintf ("%0.Lf", last.value));
 
       if (*s1 != '-' && *s2 != '-')
         seq_fast (s1, s2, step.value);
index 65c9e67888bb2b70c9ea73b48b7074d19c3e371a..226e9ceb1f022a4765c562c17258299aee2eaa04 100644 (file)
--- a/src/who.c
+++ b/src/who.c
@@ -35,6 +35,7 @@
 #include "readutmp.h"
 #include "hard-locale.h"
 #include "quote.h"
+#include "xvasprintf.h"
 
 #ifdef TTY_GROUP_NAME
 # include <grp.h>
@@ -233,7 +234,6 @@ print_line (char const *user, const char state,
   char x_idle[1 + IDLESTR_LEN + 1];
   char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
   char *x_exitstr;
-  int err;
 
   mesg[1] = state;
 
@@ -253,32 +253,29 @@ print_line (char const *user, const char state,
   else
     *x_exitstr = '\0';
 
-  err = asprintf (&buf,
-                  "%-8s"
-                  "%s"
-                  " %-12s"
-                  " %-*s"
-                  "%s"
-                  "%s"
-                  " %-8s"
-                  "%s"
-                  ,
-                  user ? user : "   .",
-                  include_mesg ? mesg : "",
-                  line,
-                  time_format_width,
-                  time_str,
-                  x_idle,
-                  x_pid,
-                  /* FIXME: it's not really clear whether the following
-                     field should be in the short_output.  A strict reading
-                     of SUSv2 would suggest not, but I haven't seen any
-                     implementations that actually work that way... */
-                  comment,
-                  x_exitstr
+  buf = xasprintf ("%-8s"
+                   "%s"
+                   " %-12s"
+                   " %-*s"
+                   "%s"
+                   "%s"
+                   " %-8s"
+                   "%s"
+                   ,
+                   user ? user : "   .",
+                   include_mesg ? mesg : "",
+                   line,
+                   time_format_width,
+                   time_str,
+                   x_idle,
+                   x_pid,
+                   /* FIXME: it's not really clear whether the following
+                      field should be in the short_output.  A strict reading
+                      of SUSv2 would suggest not, but I haven't seen any
+                      implementations that actually work that way... */
+                   comment,
+                   x_exitstr
                   );
-  if (err == -1)
-    xalloc_die ();
 
   {
     /* Remove any trailing spaces.  */