simplify listfds handling - geomyidae - A small C-based gopherd. HTML git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/geomyidae/ DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit aa6b70539a2b175efadad1d5f6a771d77a92f8ef DIR parent 206c57aca0611bef65a365710aaef32529634b5c HTML Author: Hiltjo Posthuma <hiltjo@codemadness.org> Date: Sun, 11 Nov 2018 14:00:02 +0100 simplify listfds handling simplify: - closing listfds, remove unneccesary >= fd check. - use the same order of closing the log as in sighandler. - don't check for NULL, free(NULL) is valid. Signed-off-by: Christoph Lohmann <20h@r-36.net> Diffstat: M main.c | 46 ++++++++++++------------------- 1 file changed, 18 insertions(+), 28 deletions(-) --- DIR diff --git a/main.c b/main.c @@ -271,14 +271,12 @@ sighandler(int sig) close(glfd); glfd = -1; } - if (listfds != NULL) { - for (i = 0; i < nlistfds; i++) { - if (listfds[i] >= 0) { - shutdown(listfds[i], SHUT_RDWR); - close(listfds[i]); - } - } + + for (i = 0; i < nlistfds; i++) { + shutdown(listfds[i], SHUT_RDWR); + close(listfds[i]); } + free(listfds); exit(0); break; default: @@ -583,11 +581,9 @@ main(int argc, char *argv[]) sizeof(*listfds) * ++nlistfds); listfds[nlistfds-1] = lfdret[j]; } - if (lfdret != NULL) - free(lfdret); + free(lfdret); } - if (bindips != NULL) - free(bindips); + free(bindips); if (nlistfds < 1) return 1; @@ -613,15 +609,12 @@ main(int argc, char *argv[]) if (dropprivileges(gr, us) < 0) { perror("dropprivileges"); - if (listfds != NULL) { - for (i = 0; i < nlistfds; i++) { - if (listfds[i] >= 0) { - shutdown(listfds[i], SHUT_RDWR); - close(listfds[i]); - } - } - free(listfds); + + for (i = 0; i < nlistfds; i++) { + shutdown(listfds[i], SHUT_RDWR); + close(listfds[i]); } + free(listfds); return 1; } @@ -734,21 +727,18 @@ main(int argc, char *argv[]) close(sock); } - if (listfds != NULL) { - for (i = 0; i < nlistfds; i++) { - if (listfds[i] >= 0) { - shutdown(listfds[i], SHUT_RDWR); - close(listfds[i]); - } - } - free(listfds); - } if (logfile != NULL && glfd != -1) { close(glfd); glfd = -1; } free(ohost); + for (i = 0; i < nlistfds; i++) { + shutdown(listfds[i], SHUT_RDWR); + close(listfds[i]); + } + free(listfds); + return 0; }