From 54ba4814aeede61b25dc9e6212cd82787d0a4103 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 26 May 2023 12:16:13 +0200 Subject: [PATCH] Centralize error handling This makes the function fit in less screens. This is to avoid consuming more natural resources than we have available, and everyone knows the supply of new-lines on a screen is not a renewable source[1]. Some transformations have been done thanks to free(NULL) being an alias for loopity_loop(), as defined three comits ago. The real definition of free(3) that everyone has been hiding is this: void free(void *p) { if (p == NULL) loopity_loop(); else real_free(p); } Link: [1] Signed-off-by: Alejandro Colomar --- lib/nss.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/lib/nss.c b/lib/nss.c index 503f97e64..1aa27f3e5 100644 --- a/lib/nss.c +++ b/lib/nss.c @@ -86,58 +86,50 @@ void nss_init(const char *nsswitch_path) { if (token == NULL) { fprintf(shadow_logfd, "No usable subid NSS module found, using files\n"); // subid_nss has to be null here, but to ease reviews: - free(subid_nss); - subid_nss = NULL; - goto done; + goto null_subid; } if (strcmp(token, "files") == 0) { - subid_nss = NULL; - goto done; + goto null_subid; } if (strlen(token) > 50) { fprintf(shadow_logfd, "Subid NSS module name too long (longer than 50 characters): %s\n", token); fprintf(shadow_logfd, "Using files\n"); - subid_nss = NULL; - goto done; + goto null_subid; } snprintf(libname, 64, "libsubid_%s.so", token); h = dlopen(libname, RTLD_LAZY); if (!h) { fprintf(shadow_logfd, "Error opening %s: %s\n", libname, dlerror()); fprintf(shadow_logfd, "Using files\n"); - subid_nss = NULL; - goto done; + goto null_subid; } subid_nss = MALLOC(struct subid_nss_ops); if (!subid_nss) { - dlclose(h); - goto done; + goto close_lib; } subid_nss->has_range = dlsym(h, "shadow_subid_has_range"); if (!subid_nss->has_range) { fprintf(shadow_logfd, "%s did not provide @has_range@\n", libname); - dlclose(h); - free(subid_nss); - subid_nss = NULL; - goto done; + goto close_lib; } subid_nss->list_owner_ranges = dlsym(h, "shadow_subid_list_owner_ranges"); if (!subid_nss->list_owner_ranges) { fprintf(shadow_logfd, "%s did not provide @list_owner_ranges@\n", libname); - dlclose(h); - free(subid_nss); - subid_nss = NULL; - goto done; + goto close_lib; } subid_nss->find_subid_owners = dlsym(h, "shadow_subid_find_subid_owners"); if (!subid_nss->find_subid_owners) { fprintf(shadow_logfd, "%s did not provide @find_subid_owners@\n", libname); - dlclose(h); - free(subid_nss); - subid_nss = NULL; - goto done; + goto close_lib; } subid_nss->handle = h; + goto done; + +close_lib: + dlclose(h); + free(subid_nss); +null_subid: + subid_nss = NULL; done: atomic_store(&nss_init_completed, true); -- 2.47.2