From fc48cdf6a9afbb4abe5b642ea1acbc1dfe1c6cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Thu, 3 Oct 2019 13:10:08 +0200 Subject: [PATCH] lib/lwres/getipnode.c: Resolve possible Null pointer dereference (from Cppcheck) --- lib/lwres/getipnode.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/lwres/getipnode.c b/lib/lwres/getipnode.c index c2110756d0..b185b2cb78 100644 --- a/lib/lwres/getipnode.c +++ b/lib/lwres/getipnode.c @@ -842,7 +842,7 @@ copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num) struct hostent *he = NULL; int addresses = 1; /* NULL terminator */ int names = 1; /* NULL terminator */ - int len = 0; + char *cp_name; char **cpp, **npp; /* @@ -949,14 +949,17 @@ copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num) * Copy aliases. */ npp = he->h_aliases; - cpp = (he1 != NULL) ? he1->h_aliases - : ((he2 != NULL) ? he2->h_aliases : NULL); + if (he1 != NULL) { + cpp = he1->h_aliases; + } else if (he2 != NULL) { + cpp = he2->h_aliases; + } else { + cpp = NULL; + } while (cpp != NULL && *cpp != NULL) { - len = strlen (*cpp) + 1; - *npp = malloc(len); + *npp = strdup(*cpp); if (*npp == NULL) goto cleanup2; - strcpy(*npp, *cpp); npp++; cpp++; } @@ -964,11 +967,17 @@ copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num) /* * Copy hostname. */ - he->h_name = malloc(strlen((he1 != NULL) ? - he1->h_name : he2->h_name) + 1); - if (he->h_name == NULL) + if (he1 != NULL) { + cp_name = he1->h_name; + } else if (he2 != NULL) { + cp_name = he2->h_name; + } else { goto cleanup2; - strcpy(he->h_name, (he1 != NULL) ? he1->h_name : he2->h_name); + } + he->h_name = strdup(cp_name); + if (he->h_name == NULL) { + goto cleanup2; + } /* * Set address type and length.