tests: Do not dreference key before NULL check in string_in_list()
authorAndreas Schneider <asn@samba.org>
Wed, 16 Dec 2015 14:13:59 +0000 (15:13 +0100)
committerAndreas Schneider <asn@samba.org>
Fri, 8 Jan 2016 12:07:36 +0000 (13:07 +0100)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Guenther Deschner <gd@samba.org>
tests/test_pam_wrapper.c

index c7837c9727483464d5fa843425b31fe89e697d7f..74f3c8471dec64dbcabbe8c6262765aba569b88b 100644 (file)
@@ -403,16 +403,18 @@ static void test_pam_env_functions(void **state)
 
 static const char *string_in_list(char **list, const char *key)
 {
-       char key_eq[strlen(key)+1+1]; /* trailing NULL and '=' */
-
        if (list == NULL || key == NULL) {
                return NULL;
        }
 
-       snprintf(key_eq, sizeof(key_eq), "%s=", key);
-       for (size_t i = 0; list[i] != NULL; i++) {
-               if (strncmp(list[i], key_eq, sizeof(key_eq)-1) == 0) {
-                       return list[i] + sizeof(key_eq)-1;
+       if (strlen(key) > 0) {
+               char key_eq[strlen(key) + 1 + 1]; /* trailing = and '\0' */
+
+               snprintf(key_eq, sizeof(key_eq), "%s=", key);
+               for (size_t i = 0; list[i] != NULL; i++) {
+                       if (strncmp(list[i], key_eq, sizeof(key_eq)-1) == 0) {
+                               return list[i] + sizeof(key_eq)-1;
+                       }
                }
        }