From 4b7cacc479df1af5fbb6abac483bf4aa9fa89177 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 12 Jul 2023 11:41:37 +0900 Subject: [PATCH 1/7] PR to #104. Memory allocation fail then return NULL. --- src/utilities/qstring.c | 10 ++++++++++ src/utilities/qtime.c | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/src/utilities/qstring.c b/src/utilities/qstring.c index 8ae1a084..60292313 100644 --- a/src/utilities/qstring.c +++ b/src/utilities/qstring.c @@ -251,6 +251,9 @@ char *qstrreplace(const char *mode, char *srcstr, const char *tokstr, if (method == 't') { /* Token replace */ maxstrlen = strlen(srcstr) * ((strlen(word) > 0) ? strlen(word) : 1); newstr = (char *) malloc(maxstrlen + 1); + if (newstr == NULL) + return NULL; + for (srcp = (char *) srcstr, newp = newstr; *srcp; srcp++) { for (tokenp = (char *) tokstr; *tokenp; tokenp++) { @@ -274,6 +277,9 @@ char *qstrreplace(const char *mode, char *srcstr, const char *tokstr, maxstrlen = strlen(srcstr); } newstr = (char *) malloc(maxstrlen + 1); + if (newstr == NULL) + return NULL; + tokstrlen = strlen(tokstr); for (srcp = srcstr, newp = newstr; *srcp; srcp++) { @@ -389,6 +395,9 @@ char *qstrdup_between(const char *str, const char *start, const char *end) { int len = e - s; char *buf = (char *) malloc(sizeof(char) * (len + 1)); + if (buf == NULL) + return NULL; + strncpy(buf, s, len); buf[len] = '\0'; @@ -869,6 +878,7 @@ char *qstr_conv_encoding(const char *str, const char *fromcode, char *tostr = (char *) malloc(tosize); if (tostr == NULL) return NULL; + char *tostr1 = tostr; iconv_t it = iconv_open(tocode, fromcode); diff --git a/src/utilities/qtime.c b/src/utilities/qtime.c index 1b456942..7a73d0b0 100644 --- a/src/utilities/qtime.c +++ b/src/utilities/qtime.c @@ -103,6 +103,9 @@ char *qtime_localtime_strf(char *buf, int size, time_t utctime, char *qtime_localtime_str(time_t utctime) { int size = sizeof(char) * (CONST_STRLEN("00-Jan-0000 00:00:00 +0000") + 1); char *timestr = (char *) malloc(size); + if (timestr == NULL) + return NULL; + qtime_localtime_strf(timestr, size, utctime, "%d-%b-%Y %H:%M:%S %z"); return timestr; } @@ -172,6 +175,9 @@ char *qtime_gmt_str(time_t utctime) { int size = sizeof(char) * (CONST_STRLEN("Mon, 00 Jan 0000 00:00:00 GMT") + 1); char *timestr = (char *) malloc(size); + if (timestr == NULL) + return NULL; + qtime_gmt_strf(timestr, size, utctime, "%a, %d %b %Y %H:%M:%S GMT"); return timestr; } From dde246dbc27d720f43843dc88645e16906591619 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 12 Jul 2023 12:03:54 +0900 Subject: [PATCH 2/7] fix compile warning utilities/qio.c qio_puts func strncpy(newstr, str, strsize) => memcpy Because strsize is computed in 302 line, if call strncpy then recompute len. --- src/utilities/qio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utilities/qio.c b/src/utilities/qio.c index bfbea619..e8b7b7e1 100644 --- a/src/utilities/qio.c +++ b/src/utilities/qio.c @@ -303,7 +303,7 @@ ssize_t qio_puts(int fd, const char *str, int timeoutms) { char *newstr = (char *) malloc(strsize + 1 + 1); if (newstr == NULL) return -1; - strncpy(newstr, str, strsize); + memcpy(newstr, str, strsize); newstr[strsize] = '\n'; newstr[strsize + 1] = '\0'; ssize_t ret = qio_write(fd, newstr, strsize + 1, timeoutms); From 4a652e7927e3fc611befe576101295aa5e5b0c04 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 12 Jul 2023 12:14:32 +0900 Subject: [PATCH 3/7] fix compile warning utilities/qsocket.c qsocket_close --- src/utilities/qsocket.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utilities/qsocket.c b/src/utilities/qsocket.c index 127e7942..c33fef1f 100644 --- a/src/utilities/qsocket.c +++ b/src/utilities/qsocket.c @@ -112,8 +112,9 @@ bool qsocket_close(int sockfd, int timeoutms) { char buf[1024]; while (true) { ssize_t read = qio_read(sockfd, buf, sizeof(buf), timeoutms); + DEBUG("Throw %zu bytes from dummy input stream.", read); if (read <= 0) - break;DEBUG("Throw %zu bytes from dummy input stream.", read); + break; } } From b7c785998139dcef5621424b1aa10698e83df720 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 12 Jul 2023 13:55:52 +0900 Subject: [PATCH 4/7] lined up utilities/qsocket.c --- src/utilities/qsocket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utilities/qsocket.c b/src/utilities/qsocket.c index c33fef1f..f963a311 100644 --- a/src/utilities/qsocket.c +++ b/src/utilities/qsocket.c @@ -112,7 +112,7 @@ bool qsocket_close(int sockfd, int timeoutms) { char buf[1024]; while (true) { ssize_t read = qio_read(sockfd, buf, sizeof(buf), timeoutms); - DEBUG("Throw %zu bytes from dummy input stream.", read); + DEBUG("Throw %zu bytes from dummy input stream.", read); if (read <= 0) break; } From 5a787dc84335227053ec370d106ef05c9e33e48f Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 12 Jul 2023 15:07:45 +0900 Subject: [PATCH 5/7] tab to space --- src/utilities/qstring.c | 16 +++++----------- src/utilities/qtime.c | 8 ++++---- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/utilities/qstring.c b/src/utilities/qstring.c index 60292313..eda12ddb 100644 --- a/src/utilities/qstring.c +++ b/src/utilities/qstring.c @@ -251,9 +251,8 @@ char *qstrreplace(const char *mode, char *srcstr, const char *tokstr, if (method == 't') { /* Token replace */ maxstrlen = strlen(srcstr) * ((strlen(word) > 0) ? strlen(word) : 1); newstr = (char *) malloc(maxstrlen + 1); - if (newstr == NULL) - return NULL; - + if (newstr == NULL) + return NULL; for (srcp = (char *) srcstr, newp = newstr; *srcp; srcp++) { for (tokenp = (char *) tokstr; *tokenp; tokenp++) { @@ -277,8 +276,8 @@ char *qstrreplace(const char *mode, char *srcstr, const char *tokstr, maxstrlen = strlen(srcstr); } newstr = (char *) malloc(maxstrlen + 1); - if (newstr == NULL) - return NULL; + if (newstr == NULL) + return NULL; tokstrlen = strlen(tokstr); @@ -394,13 +393,8 @@ char *qstrdup_between(const char *str, const char *start, const char *end) { int len = e - s; - char *buf = (char *) malloc(sizeof(char) * (len + 1)); - if (buf == NULL) - return NULL; - - strncpy(buf, s, len); + char *buf = qmemdup(s, len); buf[len] = '\0'; - return buf; } diff --git a/src/utilities/qtime.c b/src/utilities/qtime.c index 7a73d0b0..4434019c 100644 --- a/src/utilities/qtime.c +++ b/src/utilities/qtime.c @@ -103,8 +103,8 @@ char *qtime_localtime_strf(char *buf, int size, time_t utctime, char *qtime_localtime_str(time_t utctime) { int size = sizeof(char) * (CONST_STRLEN("00-Jan-0000 00:00:00 +0000") + 1); char *timestr = (char *) malloc(size); - if (timestr == NULL) - return NULL; + if (timestr == NULL) + return NULL; qtime_localtime_strf(timestr, size, utctime, "%d-%b-%Y %H:%M:%S %z"); return timestr; @@ -175,8 +175,8 @@ char *qtime_gmt_str(time_t utctime) { int size = sizeof(char) * (CONST_STRLEN("Mon, 00 Jan 0000 00:00:00 GMT") + 1); char *timestr = (char *) malloc(size); - if (timestr == NULL) - return NULL; + if (timestr == NULL) + return NULL; qtime_gmt_strf(timestr, size, utctime, "%a, %d %b %Y %H:%M:%S GMT"); return timestr; From 701e136a03cfd074aa52c8efae8c15721844115a Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 12 Jul 2023 15:32:05 +0900 Subject: [PATCH 6/7] qmemdup => memcpy utilities/qstring.c --- src/utilities/qstring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utilities/qstring.c b/src/utilities/qstring.c index eda12ddb..58e1c7d0 100644 --- a/src/utilities/qstring.c +++ b/src/utilities/qstring.c @@ -393,7 +393,11 @@ char *qstrdup_between(const char *str, const char *start, const char *end) { int len = e - s; - char *buf = qmemdup(s, len); + char *buf = (char *) malloc(sizeof(char) * (len + 1)); + if (buf == NULL) + return NULL; + + memcpy(buf, s, len); buf[len] = '\0'; return buf; } From 6a29214ae1ecf5823593b33b677e1c3e3ac93ac2 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 12 Jul 2023 16:02:00 +0900 Subject: [PATCH 7/7] revert memcpy -> strncpy memcpy => strncpy src/utilities/qio.c :306 src/utilities/qstring.c :400 --- src/utilities/qio.c | 2 +- src/utilities/qstring.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utilities/qio.c b/src/utilities/qio.c index e8b7b7e1..bfbea619 100644 --- a/src/utilities/qio.c +++ b/src/utilities/qio.c @@ -303,7 +303,7 @@ ssize_t qio_puts(int fd, const char *str, int timeoutms) { char *newstr = (char *) malloc(strsize + 1 + 1); if (newstr == NULL) return -1; - memcpy(newstr, str, strsize); + strncpy(newstr, str, strsize); newstr[strsize] = '\n'; newstr[strsize + 1] = '\0'; ssize_t ret = qio_write(fd, newstr, strsize + 1, timeoutms); diff --git a/src/utilities/qstring.c b/src/utilities/qstring.c index 58e1c7d0..93430f27 100644 --- a/src/utilities/qstring.c +++ b/src/utilities/qstring.c @@ -397,7 +397,7 @@ char *qstrdup_between(const char *str, const char *start, const char *end) { if (buf == NULL) return NULL; - memcpy(buf, s, len); + strncpy(buf, s, len); buf[len] = '\0'; return buf; }