roken: refactor rk_getauxval
authorJeffrey Altman <jaltman@secure-endpoints.com>
Sat, 29 Apr 2017 17:45:33 +0000 (13:45 -0400)
committerJeffrey Altman <jaltman@secure-endpoints.com>
Sat, 29 Apr 2017 17:55:25 +0000 (13:55 -0400)
Refactor rk_getauxval() to remove duplicate code and to ensure
that a value is always returned.

Change-Id: I3b452dbc11802169e2c96e7ad16e714e7a880450

lib/roken/getauxval.c

index 31f52fa6ec4edbf988cf0d34edbf06ef1599c5b5..2fab3763822928384ec103a6f0436ef8f76d1598 100644 (file)
@@ -157,12 +157,12 @@ rk_getprocauxval(unsigned long type)
 ROKEN_LIB_FUNCTION unsigned long ROKEN_LIB_CALL
 rk_getauxval(unsigned long type)
 {
-#ifdef HAVE_GETAUXVAL
-#ifdef GETAUXVAL_SETS_ERRNO
+#if defined(HAVE_GETAUXVAL) && defined(GETAUXVAL_SETS_ERRNO)
     if (rk_injected_auxv)
         return rk_getprocauxval(type);
     return getauxval(type);
 #else
+    const auxv_t *a;
     unsigned long ret;
     unsigned long ret2;
     static int getauxval_sets_errno = -1;
@@ -181,17 +181,8 @@ rk_getauxval(unsigned long type)
         return ret;
     }
 
-    if (getauxval_sets_errno == 0) {
-       const auxv_t *a;
-
-        errno = save_errno;
-       a = rk_getauxv(type);
-       if (a == NULL) {
-            errno = ENOENT;
-            return 0;
-        }
-        return a->a_un.a_val;
-    }
+    if (getauxval_sets_errno == 0) 
+       goto out;
 
     /*
      * We've called getauxval() and it returned 0, but we don't know if
@@ -200,7 +191,6 @@ rk_getauxval(unsigned long type)
      * Attempt to detect whether getauxval() sets errno = ENOENT by
      * calling it with what should be a bogus type.
      */
-
     errno = 0;
     ret2 = getauxval(~type);
     if (ret2 == 0 && errno == ENOENT) {
@@ -208,16 +198,14 @@ rk_getauxval(unsigned long type)
         errno = save_errno;
         return ret;
     }
-
     getauxval_sets_errno = 0;
-    errno = save_errno;
-#endif
-#else
-    const auxv_t *a;
 
-    if ((a = rk_getauxv(type)) == NULL) {
-        errno = ENOENT;
-        return 0;
+  out:
+    errno = save_errno;
+    a = rk_getauxv(type);
+    if (a == NULL) {
+       errno = ENOENT;
+       return 0;
     }
     return a->a_un.a_val;
 #endif