From 9e2e59c8b84fcf5169c9b3730aab1c929263e78b Mon Sep 17 00:00:00 2001 From: zzulilyw Date: Mon, 7 Aug 2023 03:27:23 +0000 Subject: [PATCH] Fix irregular code writing Issue: https://gitee.com/openharmony/third_party_musl/issues/I7OSP8 test: libc-test Signed-off-by: zzulilyw --- porting/linux/user/src/network/getnameinfo.c | 8 +++++--- porting/linux/user/src/network/lookup_name.c | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/porting/linux/user/src/network/getnameinfo.c b/porting/linux/user/src/network/getnameinfo.c index a73c2ea8..fe32156b 100644 --- a/porting/linux/user/src/network/getnameinfo.c +++ b/porting/linux/user/src/network/getnameinfo.c @@ -16,8 +16,10 @@ #define RR_PTR 12 #define BREAK 0 #define CONTINUE 1 +#define FIXED_HOSTS_MAX_LENGTH 2 +#define FIXED_HOSTS_STR_MAX_LENGTH 23 -extern char fixed_hosts[][23]; +extern char fixed_hosts[FIXED_HOSTS_MAX_LENGTH][FIXED_HOSTS_STR_MAX_LENGTH]; static char *itoa(char *p, unsigned x) { p += 3*sizeof(int); @@ -51,7 +53,7 @@ static inline int get_hosts_str(char *line, int length, FILE *f, int *i) if (f) { return fgets(line, sizeof line, f); } - if (*i < 2) { + if (*i < FIXED_HOSTS_MAX_LENGTH) { memcpy(line, fixed_hosts[*i], strlen(fixed_hosts[*i])); (*i)++; return 1; @@ -71,7 +73,7 @@ static void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, i a = atmp; } int i = 0; - while (i < 2 && get_hosts_str(line, sizeof line, f, &i)) { + while (i < FIXED_HOSTS_MAX_LENGTH && get_hosts_str(line, sizeof line, f, &i)) { if ((p=strchr(line, '#'))) *p++='\n', *p=0; for (p=line; *p && !isspace(*p); p++); diff --git a/porting/linux/user/src/network/lookup_name.c b/porting/linux/user/src/network/lookup_name.c index b5a85eb2..4feb0c1f 100644 --- a/porting/linux/user/src/network/lookup_name.c +++ b/porting/linux/user/src/network/lookup_name.c @@ -19,8 +19,10 @@ #if OHOS_PERMISSION_INTERNET uint8_t is_allow_internet(void); #endif +#define FIXED_HOSTS_MAX_LENGTH 2 +#define FIXED_HOSTS_STR_MAX_LENGTH 23 -char fixed_hosts[][23] = { +char fixed_hosts[][FIXED_HOSTS_STR_MAX_LENGTH] = { "127.0.0.1 localhost\r\n\0", "::1 ip6-localhost\r\n\0" }; @@ -61,7 +63,7 @@ static inline int get_hosts_str(char *line, int length, FILE *f, int *i) if (f) { return fgets(line, sizeof line, f); } - if (*i < 2) { + if (*i < FIXED_HOSTS_MAX_LENGTH) { memcpy(line, fixed_hosts[*i], strlen(fixed_hosts[*i])); (*i)++; return 1; @@ -77,7 +79,7 @@ static int name_from_hosts(struct address buf[static MAXADDRS], char canon[stati unsigned char _buf[1032]; FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf); int i = 0; - while (i < 2 && get_hosts_str(line, sizeof line, f, &i) && cnt < MAXADDRS) { + while (i < FIXED_HOSTS_MAX_LENGTH && get_hosts_str(line, sizeof line, f, &i) && cnt < MAXADDRS) { char *p, *z; if ((p=strchr(line, '#'))) *p++='\n', *p=0; -- GitLab