From 181359bd81451f9516a2358047ec0694d0f0f34e Mon Sep 17 00:00:00 2001 From: overflowerror Date: Wed, 5 May 2021 23:03:57 +0200 Subject: [PATCH] removed strclone function (I have no idea why I added it in the first place) --- src/config.c | 16 ++++++++-------- src/headers.c | 4 ++-- src/networking.c | 15 +++++++++++---- src/test.c | 5 +---- src/util.c | 8 -------- src/util.h | 2 -- 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/config.c b/src/config.c index 50259cf..4cb7478 100644 --- a/src/config.c +++ b/src/config.c @@ -166,9 +166,9 @@ struct config* config_parse(FILE* file) { char* host; if (strcmp(currentToken, "*") == 0) - host = strclone("0.0.0.0"); + host = strdup("0.0.0.0"); else - host = strclone(currentToken); + host = strdup(currentToken); if (host == NULL) { error("config: error cloning host string"); @@ -177,7 +177,7 @@ struct config* config_parse(FILE* file) { } replaceOrAdd(toFree, &toFreeLength, NULL, host); - char* port = strclone(seperator + 1); + char* port = strdup(seperator + 1); if (port == NULL) { error("config: error cloning port string"); freeEverything(toFree, toFreeLength); @@ -397,7 +397,7 @@ struct config* config_parse(FILE* file) { replaceOrAdd(toFree, &toFreeLength, currentSite->hostnames, tmpArray); currentSite->hostnames = tmpArray; - tmp = strclone(currentToken); + tmp = strdup(currentToken); if (tmp == NULL) { error("config: error cloning hostname string"); freeEverything(toFree, toFreeLength); @@ -431,7 +431,7 @@ struct config* config_parse(FILE* file) { state = SITE_CONTENT; break; case HANDLER_VALUE: - currentHandler->dir = strclone(currentToken); + currentHandler->dir = strdup(currentToken); if (currentHandler->dir == NULL) { error("config: error cloning handle directory string"); freeEverything(toFree, toFreeLength); @@ -509,7 +509,7 @@ struct config* config_parse(FILE* file) { replaceOrAdd(toFree, &toFreeLength, settings->indexfiles.files, tmpArray); settings->indexfiles.files = tmpArray; - tmp = strclone(currentToken); + tmp = strdup(currentToken); if (tmp == NULL) { error("config: error cloning hostname string"); freeEverything(toFree, toFreeLength); @@ -554,7 +554,7 @@ struct config* config_parse(FILE* file) { state = LOGGING_ACCESS_FILE_VALUE; break; case LOGGING_ACCESS_FILE_VALUE: - config->logging.accessLogfile = strclone(currentToken); + config->logging.accessLogfile = strdup(currentToken); if (config->logging.accessLogfile == NULL) { error("config: error cloning access log file string"); freeEverything(toFree, toFreeLength); @@ -572,7 +572,7 @@ struct config* config_parse(FILE* file) { state = LOGGING_SERVER_FILE_VALUE; break; case LOGGING_SERVER_FILE_VALUE: - config->logging.serverLogfile = strclone(currentToken); + config->logging.serverLogfile = strdup(currentToken); if (config->logging.serverLogfile == NULL) { error("config: error cloning server log file string"); freeEverything(toFree, toFreeLength); diff --git a/src/headers.c b/src/headers.c index 3d81e68..b1681a0 100644 --- a/src/headers.c +++ b/src/headers.c @@ -46,12 +46,12 @@ int headers_remove(struct headers* headers, const char* key) { } int headers_mod(struct headers* headers, const char* _key, const char* _value) { - char* tmp = strclone(_key); + char* tmp = strdup(_key); if (tmp == NULL) { return HEADERS_ALLOC_ERROR; } char* key = tmp; - tmp = strclone(_value); + tmp = strdup(_value); if (tmp == NULL) { return HEADERS_ALLOC_ERROR; } diff --git a/src/networking.c b/src/networking.c index b31686d..ef3f57a 100644 --- a/src/networking.c +++ b/src/networking.c @@ -683,14 +683,21 @@ void* listenThread(void* _bind) { gethostbyaddr_r(&client, sizeof(client), family, &entry, &(buffer[0]), LOCAL_BUFFER_SIZE, &result, &h_errno); if (result == NULL) { - peer.name = strclone(""); + peer.name = strdup(""); } else { - peer.name = strclone(entry.h_name); + peer.name = strdup(entry.h_name); if (peer.name == NULL) { - error("networking: Couldn't strclone hostname: %s", strerror(errno)); - peer.name = strclone(""); + error("networking: Couldn't strdup hostname: %s", strerror(errno)); + peer.name = strdup(""); } } + + if (peer.name == NULL) { + close(tmp); + free(connection); + error("networking: bad: strdup on empty string: %s", strerror(errno)); + continue; + } snprintf(&(peer.portStr[0]), 5 + 1, "%d", peer.port); diff --git a/src/test.c b/src/test.c index 4ffc198..bb4775e 100644 --- a/src/test.c +++ b/src/test.c @@ -53,10 +53,7 @@ void showError() { void testUtil() { const char* original = "Hello World"; - char* clone = strclone(original); - - checkNull(clone, "clone: null check"); - checkString(clone, original, "clone: value check"); + char* clone = strdup(original); strremove(clone, 4, 1); checkString(clone, "Hell World", "remove: middle"); diff --git a/src/util.c b/src/util.c index 1117375..d8036b4 100644 --- a/src/util.c +++ b/src/util.c @@ -180,14 +180,6 @@ void* fileCopyThread(void* data) { return NULL; } -char* strclone(const char* string) { - char* result = malloc(strlen(string) + 1); - if (result == NULL) - return NULL; - strcpy(result, string); - return result; -} - int strlenOfNumber(long long number) { int result = 1; diff --git a/src/util.h b/src/util.h index 129ee43..4a76a7b 100644 --- a/src/util.h +++ b/src/util.h @@ -18,8 +18,6 @@ struct fileCopy { int startCopyThread(int from, int to, bool closeWriteFd, pthread_t* thread); void* fileCopyThread(void* data); -char* strclone(const char* string); - int strlenOfNumber(long long number); char* getTimestamp();