From faaedd342f80c8febc58246c003f5cf75dafe0b8 Mon Sep 17 00:00:00 2001 From: Amin Ben Ramdhane Date: Tue, 25 May 2021 00:14:44 +0100 Subject: [PATCH] Fix x86 compilation error using GCC 9 --- cwmp_uci.c | 4 ++-- http.c | 4 ++-- inc/common.h | 6 ++++++ log.c | 10 +++++----- xml.c | 16 ++++++++-------- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/cwmp_uci.c b/cwmp_uci.c index 59ea335..dbf6325 100644 --- a/cwmp_uci.c +++ b/cwmp_uci.c @@ -272,7 +272,7 @@ int cwmp_uci_get_value_common(char *cmd, char **value, bool state) return CWMP_GEN_ERR; } if (state) { - strncpy(state_path, VARSTATE_CONFIG, strlen(VARSTATE_CONFIG)); + CWMP_STRNCPY(state_path, VARSTATE_CONFIG, sizeof(state_path)); uci_add_delta_path(c, c->savedir); uci_set_savedir(c, state_path); } @@ -351,7 +351,7 @@ int uci_set_value(char *path, char *value, uci_config_action action) } if (action == CWMP_CMD_SET_STATE) { - strncpy(state_path, VARSTATE_CONFIG, strlen(VARSTATE_CONFIG)); + CWMP_STRNCPY(state_path, VARSTATE_CONFIG, sizeof(state_path)); uci_add_delta_path(c, c->savedir); uci_set_savedir(c, state_path); } diff --git a/http.c b/http.c index 3d91e25..246a411 100644 --- a/http.c +++ b/http.c @@ -220,7 +220,7 @@ int http_send_message(struct cwmp *cwmp, char *msg_out, int msg_out_len, char ** curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip); if (ip && ip[0] != '\0') { if (ip_acs[0] == '\0' || strcmp(ip_acs, ip) != 0) { - strncpy(ip_acs, ip, strlen(ip)); + CWMP_STRNCPY(ip_acs, ip, sizeof(ip_acs)); if (cwmp->conf.ipv6_enable) { tmp = inet_pton(AF_INET, ip, buf); if (tmp == 1) @@ -307,7 +307,7 @@ static void http_cr_new_client(int client, bool service_available) method_is_get = true; if (!strncasecmp(buffer, "Authorization: Digest ", strlen("Authorization: Digest "))) { auth_digest_checked = true; - strncpy(auth_digest_buffer, buffer, strlen(buffer)); + CWMP_STRNCPY(auth_digest_buffer, buffer, BUFSIZ); } if (buffer[0] == '\r' || buffer[0] == '\n') { diff --git a/inc/common.h b/inc/common.h index ae2956d..05dacfc 100644 --- a/inc/common.h +++ b/inc/common.h @@ -512,4 +512,10 @@ void icwmp_cleanmem(); } while (0) #endif +#define CWMP_STRNCPY(DST, SRC, SIZE) \ +do { \ + strncpy(DST, SRC, SIZE-1); \ + DST[SIZE-1] = '\0'; \ +} while(0) + #endif diff --git a/log.c b/log.c index 7470fd1..cb67989 100644 --- a/log.c +++ b/log.c @@ -42,9 +42,9 @@ int log_set_severity_idx(char *value) int log_set_log_file_name(char *value) { if (value != NULL) { - strncpy(log_file_name, value, strlen(value)); + CWMP_STRNCPY(log_file_name, value, sizeof(log_file_name)); } else { - strncpy(log_file_name, DEFAULT_LOG_FILE_NAME, strlen(DEFAULT_LOG_FILE_NAME)); + CWMP_STRNCPY(log_file_name, DEFAULT_LOG_FILE_NAME, sizeof(log_file_name)); } return 1; } @@ -121,7 +121,7 @@ void puts_log(int severity, const char *fmt, ...) Tm = localtime(&tv.tv_sec); i = snprintf(buf, sizeof(buf), "%02d-%02d-%4d, %02d:%02d:%02d %s ", Tm->tm_mday, Tm->tm_mon + 1, Tm->tm_year + 1900, Tm->tm_hour, Tm->tm_min, Tm->tm_sec, SEVERITY_NAMES[severity]); if (strlen(log_file_name) == 0) { - strncpy(log_file_name, DEFAULT_LOG_FILE_NAME, strlen(DEFAULT_LOG_FILE_NAME)); + CWMP_STRNCPY(log_file_name, DEFAULT_LOG_FILE_NAME, sizeof(log_file_name)); } if (enable_log_file) { if (stat(log_file_name, &st) == 0) { @@ -138,7 +138,7 @@ void puts_log(int severity, const char *fmt, ...) va_start(args, fmt); i += vsprintf(buf + i, (const char *)fmt, args); if (enable_log_file) { - strncpy(buf_file, buf, strlen(buf)); + CWMP_STRNCPY(buf_file, buf, sizeof(buf_file)); buf_file[strlen(buf)] = '\n'; buf_file[strlen(buf) + 1] = '\0'; fputs(buf_file, pLog); @@ -184,7 +184,7 @@ void puts_log_xmlmsg(int severity, char *msg, int msgtype) Tm = localtime(&tv.tv_sec); snprintf(buf, sizeof(buf), "%02d-%02d-%4d, %02d:%02d:%02d %s ", Tm->tm_mday, Tm->tm_mon + 1, Tm->tm_year + 1900, Tm->tm_hour, Tm->tm_min, Tm->tm_sec, SEVERITY_NAMES[severity]); if (strlen(log_file_name) == 0) { - strncpy(log_file_name, DEFAULT_LOG_FILE_NAME, strlen(DEFAULT_LOG_FILE_NAME)); + CWMP_STRNCPY(log_file_name, DEFAULT_LOG_FILE_NAME, sizeof(log_file_name)); } if (msgtype == XML_MSG_IN) { diff --git a/xml.c b/xml.c index ee4d754..9e35260 100755 --- a/xml.c +++ b/xml.c @@ -654,18 +654,18 @@ error: char *xml_get_cwmp_version(int version) { + static char versions[60]; + unsigned pos = 0; int k; - char tmp[15] = ""; - static char versions[60] = ""; - versions[0] = '\0'; + versions[0] = '\0'; for (k = 0; k < version; k++) { - if (k == 0) - sprintf(tmp, "1.%d", k); - else - sprintf(tmp, ", 1.%d", k); - strncat(versions, tmp, strlen(tmp)); + pos += snprintf(&versions[pos], sizeof(versions) - pos, "1.%d, ", k); } + + if (pos) + versions[pos - 2] = 0; + return versions; }