diff toys/ls.c @ 650:dd82e0b28eda

Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
author Rob Landley <rob@landley.net>
date Sat, 18 Aug 2012 21:12:02 -0500
parents 92200901cfe1
children
line wrap: on
line diff
--- a/toys/ls.c	Thu Aug 16 22:24:30 2012 -0500
+++ b/toys/ls.c	Sat Aug 18 21:12:02 2012 -0500
@@ -88,6 +88,9 @@
 
     unsigned screen_width;
     int nl_title;
+
+    // group and user can make overlapping use of the utoa() buf, so move it
+    char uid_buf[12];
 )
 
 #define TT this.ls
@@ -121,7 +124,8 @@
 static char *getusername(uid_t uid)
 {
     struct passwd *pw = getpwuid(uid);
-    return pw ? pw->pw_name : utoa(uid);
+    utoa_to_buf(uid, TT.uid_buf, 12);
+    return pw ? pw->pw_name : TT.uid_buf;
 }
 
 static char *getgroupname(gid_t gid)
@@ -365,7 +369,7 @@
 
         if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
             struct tm *tm;
-            char perm[11], thyme[64], c, d, *usr, *upad, buf[12], *grp, *grpad;
+            char perm[11], thyme[64], c, d, *usr, *upad, *grp, *grpad;
             int i, bit;
 
             perm[10]=0;
@@ -402,8 +406,8 @@
             else {
                 upad = toybuf+255-(totals[3]-len[3]);
                 if (flags&FLAG_n) {
-                    usr = buf;
-                    utoa_to_buf(st->st_uid, buf, 12);
+                    usr = TT.uid_buf;
+                    utoa_to_buf(st->st_uid, TT.uid_buf, 12);
                 } else usr = getusername(st->st_uid);
             }