Merge "init/util.cpp: don't return a negative unsigned value"

This commit is contained in:
Nick Kralevich 2015-06-19 03:24:17 +00:00 committed by Gerrit Code Review
commit d2e7f0021d
2 changed files with 19 additions and 11 deletions

View file

@ -47,7 +47,7 @@
/* /*
* android_name_to_id - returns the integer uid/gid associated with the given * android_name_to_id - returns the integer uid/gid associated with the given
* name, or -1U on error. * name, or UINT_MAX on error.
*/ */
static unsigned int android_name_to_id(const char *name) static unsigned int android_name_to_id(const char *name)
{ {
@ -59,27 +59,35 @@ static unsigned int android_name_to_id(const char *name)
return info[n].aid; return info[n].aid;
} }
return -1U; return UINT_MAX;
} }
/* static unsigned int do_decode_uid(const char *s)
* decode_uid - decodes and returns the given string, which can be either the
* numeric or name representation, into the integer uid or gid. Returns -1U on
* error.
*/
unsigned int decode_uid(const char *s)
{ {
unsigned int v; unsigned int v;
if (!s || *s == '\0') if (!s || *s == '\0')
return -1U; return UINT_MAX;
if (isalpha(s[0])) if (isalpha(s[0]))
return android_name_to_id(s); return android_name_to_id(s);
errno = 0; errno = 0;
v = (unsigned int) strtoul(s, 0, 0); v = (unsigned int) strtoul(s, 0, 0);
if (errno) if (errno)
return -1U; return UINT_MAX;
return v;
}
/*
* decode_uid - decodes and returns the given string, which can be either the
* numeric or name representation, into the integer uid or gid. Returns
* UINT_MAX on error.
*/
unsigned int decode_uid(const char *s) {
unsigned int v = do_decode_uid(s);
if (v == UINT_MAX) {
ERROR("decode_uid: Unable to find UID for '%s'. Returning UINT_MAX\n", s);
}
return v; return v;
} }

View file

@ -38,6 +38,6 @@ TEST(util, read_file_success) {
TEST(util, decode_uid) { TEST(util, decode_uid) {
EXPECT_EQ(0U, decode_uid("root")); EXPECT_EQ(0U, decode_uid("root"));
EXPECT_EQ(-1U, decode_uid("toot")); EXPECT_EQ(UINT_MAX, decode_uid("toot"));
EXPECT_EQ(123U, decode_uid("123")); EXPECT_EQ(123U, decode_uid("123"));
} }