From d8a60c6fdc4b2ef4048a154b189a5a0d79158929 Mon Sep 17 00:00:00 2001 From: simransinghal Date: Sat, 31 Mar 2018 14:54:35 +0530 Subject: [PATCH] common: addr_parsing: Cleanup and Refactor Code *Remove assignment in if condition. *Remove unnecessary cast on void pointer. *Add a blank line after declaration(s). *Replace explicit NULL comparison with ! operator to simplify the code. *Initialize variables in the declaration. Signed-off-by: Simran Singhal --- src/common/addr_parsing.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/common/addr_parsing.c b/src/common/addr_parsing.c index 5069440298054..2a5f56623da59 100644 --- a/src/common/addr_parsing.c +++ b/src/common/addr_parsing.c @@ -31,12 +31,13 @@ int safe_cat(char **pstr, int *plen, int pos, const char *str2) while (*plen < pos + len2 + 1) { *plen += BUF_SIZE; - void *_realloc = NULL; - if ((_realloc = realloc(*pstr, (size_t)*plen)) == NULL) { + void *_realloc = realloc(*pstr, (size_t)*plen); + + if (!_realloc) { printf("Out of memory\n"); exit(1); } else { - *pstr = (char *)_realloc; + *pstr = _realloc; } //printf("safe_cat '%s' max %d pos %d '%s' len %d\n", *pstr, *plen, pos, str2, len2); } @@ -49,22 +50,20 @@ int safe_cat(char **pstr, int *plen, int pos, const char *str2) char *resolve_addrs(const char *orig_str) { - char *new_str; - char *tok, *saveptr = NULL; - int len, pos; - char *buf = strdup(orig_str); - const char *delim = ",; "; + int len = BUF_SIZE; + char *new_str = (char *)malloc(len); - len = BUF_SIZE; - new_str = (char *)malloc(len); if (!new_str) { - free(buf); return NULL; } - pos = 0; + char *saveptr = NULL; + char *buf = strdup(orig_str); + const char *delim = ",; "; + + char *tok = strtok_r(buf, delim, &saveptr); - tok = strtok_r(buf, delim, &saveptr); + int pos = 0; while (tok) { struct addrinfo hint; -- 2.39.5