[Toybox] [PATCH 1/2] ls -l: fix a bug when, without -n, both uid and gid are unavailable.

Avery Pennarun apenwarr at gmail.com
Wed Aug 15 20:56:05 PDT 2012


In that case, the same utoa() buffer was used twice, so it would show the
uid number in place of the gid.
---
 lib/lib.c |   12 +++++++-----
 lib/lib.h |    4 ++--
 toys/ls.c |    6 ++++--
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/lib/lib.c b/lib/lib.c
index e7dc45e..dc05c57 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -463,9 +463,10 @@ struct string_list *find_in_path(char *path, char *filename)
 // Convert unsigned int to ascii, writing into supplied buffer.  A truncated
 // result contains the first few digits of the result ala strncpy, and is
 // always null terminated (unless buflen is 0).
-void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
+char *utoa_to_buf(unsigned n, char *buf, unsigned buflen)
 {
 	int i, out = 0;
+	char *p = buf;
 
 	if (buflen) {
 		for (i=1000000000; i; i/=10) {
@@ -474,22 +475,23 @@ void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
 			if ((res || out || i == 1) && --buflen>0) {
 				out++;
 				n -= res*i;
-				*buf++ = '0' + res;
+				*p++ = '0' + res;
 			}
 		}
-		*buf = 0;
+		*p = 0;
 	}
+	return buf;
 }
 
 // Convert signed integer to ascii, using utoa_to_buf()
-void itoa_to_buf(int n, char *buf, unsigned buflen)
+char *itoa_to_buf(int n, char *buf, unsigned buflen)
 {
 	if (buflen && n<0) {
 		n = -n;
 		*buf++ = '-';
 		buflen--;
 	}
-	utoa_to_buf((unsigned)n, buf, buflen);
+	return utoa_to_buf((unsigned)n, buf, buflen);
 }
 
 // This static buffer is used by both utoa() and itoa(), calling either one a
diff --git a/lib/lib.h b/lib/lib.h
index 992d7d2..2ce74a3 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -123,8 +123,8 @@ void xchdir(char *path);
 void xmkpath(char *path, int mode);
 void xsetuid(uid_t uid);
 struct string_list *find_in_path(char *path, char *filename);
-void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
-void itoa_to_buf(int n, char *buf, unsigned buflen);
+char *utoa_to_buf(unsigned n, char *buf, unsigned buflen);
+char *itoa_to_buf(int n, char *buf, unsigned buflen);
 char *utoa(unsigned n);
 char *itoa(int n);
 long atolx(char *c);
diff --git a/toys/ls.c b/toys/ls.c
index 561b353..36b7e5a 100644
--- a/toys/ls.c
+++ b/toys/ls.c
@@ -120,14 +120,16 @@ static char endtype(struct stat *st)
 
 static char *getusername(uid_t uid)
 {
+    static char buf[12];
     struct passwd *pw = getpwuid(uid);
-    return pw ? pw->pw_name : utoa(uid);
+    return pw ? pw->pw_name : utoa_to_buf(uid, buf, sizeof(buf));
 }
 
 static char *getgroupname(gid_t gid)
 {
+    static char buf[12];
     struct group *gr = getgrgid(gid);
-    return gr ? gr->gr_name : utoa(gid);
+    return gr ? gr->gr_name : utoa_to_buf(gid, buf, sizeof(buf));
 }
 
 // Figure out size of printable entry fields for display indent/wrap
-- 
1.7.9.dirty


 1345089365.0


More information about the Toybox mailing list