diff options
author | Thomas Haller <thaller@redhat.com> | 2023-08-18 12:19:23 (GMT) |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2023-08-18 12:20:58 (GMT) |
commit | b544a3c69c8a8adf330fb243069d705bde60109c (patch) | |
tree | dd53df2656c9d147cb76326c9a1877905adb5048 | |
parent | 74bffbf6181f9d5afc722ce71c9cc9e48d519dbc (diff) | |
parent | e3e6fd6d2d3620cc5678317ef4218edf6a030907 (diff) | |
download | libnl-b544a3c69c8a8adf330fb243069d705bde60109c.zip libnl-b544a3c69c8a8adf330fb243069d705bde60109c.tar.gz libnl-b544a3c69c8a8adf330fb243069d705bde60109c.tar.bz2 |
all: merge 'th/avoid-mt-unsafe-libc'
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | include/nl-priv-dynamic-core/nl-core.h | 4 | ||||
-rw-r--r-- | lib/utils.c | 69 | ||||
-rw-r--r-- | lib/xfrm/ae.c | 5 | ||||
-rw-r--r-- | lib/xfrm/sa.c | 5 | ||||
-rw-r--r-- | lib/xfrm/sp.c | 5 | ||||
-rw-r--r-- | tests/check-direct.c | 9 | ||||
-rw-r--r-- | tests/cksuite-all-ematch-tree-clone.c | 8 | ||||
-rw-r--r-- | tests/test-cache-mngr.c | 3 |
9 files changed, 89 insertions, 23 deletions
diff --git a/configure.ac b/configure.ac index 831ca4b..ead499a 100644 --- a/configure.ac +++ b/configure.ac @@ -120,6 +120,10 @@ AC_CONFIG_SUBDIRS([doc]) AC_CHECK_FUNCS([strerror_l]) +AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r], [], [], [[ + #include <netdb.h> +]]) + AC_CONFIG_FILES([ Makefile libnl-3.0.pc diff --git a/include/nl-priv-dynamic-core/nl-core.h b/include/nl-priv-dynamic-core/nl-core.h index d7bec85..33ebfa1 100644 --- a/include/nl-priv-dynamic-core/nl-core.h +++ b/include/nl-priv-dynamic-core/nl-core.h @@ -66,6 +66,10 @@ struct nl_msg { /*****************************************************************************/ +int nl_getprotobyname(const char *name); + +bool nl_getprotobynumber(int proto, char *out_name, size_t name_len); + extern const char *nl_strerror_l(int err); extern int __nl_read_num_str_file(const char *path, diff --git a/lib/utils.c b/lib/utils.c index c46cf51..5281c41 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -128,6 +128,57 @@ struct trans_list { struct nl_list_head list; }; +int nl_getprotobyname(const char *name) +{ + const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNAME_R + struct protoent result_buf; + char buf[2048]; + int r; + + r = getprotobyname_r(name, &result_buf, buf, sizeof(buf), + (struct protoent **)&result); + if (r != 0 || result != &result_buf) + result = NULL; +#else + result = getprotobyname(name); +#endif + + if (!result) + return -1; + + if (result->p_proto < 0 || result->p_proto > UINT8_MAX) + return -1; + return (uint8_t)result->p_proto; +} + +bool nl_getprotobynumber(int proto, char *out_name, size_t name_len) +{ + const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNUMBER_R + struct protoent result_buf; + char buf[2048]; + int r; + + r = getprotobynumber_r(proto, &result_buf, buf, sizeof(buf), + (struct protoent **)&result); + if (r != 0 || result != &result_buf) + result = NULL; +#else + result = getprotobynumber(proto); +#endif + + if (!result) + return false; + + if (strlen(result->p_name) >= name_len) + return false; + strcpy(out_name, result->p_name); + return true; +} + const char *nl_strerror_l(int err) { const char *buf; @@ -870,12 +921,8 @@ int nl_str2ether_proto(const char *name) char *nl_ip_proto2str(int proto, char *buf, size_t len) { - struct protoent *p = getprotobynumber(proto); - - if (p) { - snprintf(buf, len, "%s", p->p_name); + if (nl_getprotobynumber(proto, buf, len)) return buf; - } snprintf(buf, len, "0x%x", proto); return buf; @@ -883,15 +930,19 @@ char *nl_ip_proto2str(int proto, char *buf, size_t len) int nl_str2ip_proto(const char *name) { - struct protoent *p = getprotobyname(name); unsigned long l; char *end; + int p; - if (p) - return p->p_proto; + if (!name) + return -NLE_INVAL; + + p = nl_getprotobyname(name); + if (p >= 0) + return p; l = strtoul(name, &end, 0); - if (l == ULONG_MAX || *end != '\0') + if (name == end || *end != '\0' || l > (unsigned long)INT_MAX) return -NLE_OBJ_NOTFOUND; return (int) l; diff --git a/lib/xfrm/ae.c b/lib/xfrm/ae.c index fbd669f..4f56690 100644 --- a/lib/xfrm/ae.c +++ b/lib/xfrm/ae.c @@ -335,6 +335,7 @@ static void xfrm_ae_dump_line(struct nl_object *a, struct nl_dump_params *p) char flags[128], buf[128]; time_t add_time, use_time; struct tm *add_time_tm, *use_time_tm; + struct tm tm_buf; nl_dump_line(p, "src %s dst %s \n", nl_addr2str(ae->saddr, src, sizeof(src)), nl_addr2str(ae->sa_id.daddr, dst, sizeof(dst))); @@ -354,7 +355,7 @@ static void xfrm_ae_dump_line(struct nl_object *a, struct nl_dump_params *p) if (ae->lifetime_cur.add_time != 0) { add_time = ae->lifetime_cur.add_time; - add_time_tm = gmtime (&add_time); + add_time_tm = gmtime_r (&add_time, &tm_buf); strftime (flags, 128, "%Y-%m-%d %H-%M-%S", add_time_tm); } else @@ -365,7 +366,7 @@ static void xfrm_ae_dump_line(struct nl_object *a, struct nl_dump_params *p) if (ae->lifetime_cur.use_time != 0) { use_time = ae->lifetime_cur.use_time; - use_time_tm = gmtime (&use_time); + use_time_tm = gmtime_r (&use_time, &tm_buf); strftime (buf, 128, "%Y-%m-%d %H-%M-%S", use_time_tm); } else diff --git a/lib/xfrm/sa.c b/lib/xfrm/sa.c index 0c1f778..44faa89 100644 --- a/lib/xfrm/sa.c +++ b/lib/xfrm/sa.c @@ -502,6 +502,7 @@ static void xfrm_sa_dump_line(struct nl_object *a, struct nl_dump_params *p) char flags[128], mode[128]; time_t add_time, use_time; struct tm *add_time_tm, *use_time_tm; + struct tm tm_buf; nl_dump_line(p, "src %s dst %s family: %s\n", nl_addr2str(sa->saddr, src, sizeof(src)), nl_addr2str(sa->id.daddr, dst, sizeof(dst)), @@ -554,7 +555,7 @@ static void xfrm_sa_dump_line(struct nl_object *a, struct nl_dump_params *p) if (sa->curlft.add_time != 0) { add_time = sa->curlft.add_time; - add_time_tm = gmtime (&add_time); + add_time_tm = gmtime_r (&add_time, &tm_buf); strftime (flags, 128, "%Y-%m-%d %H-%M-%S", add_time_tm); } else @@ -565,7 +566,7 @@ static void xfrm_sa_dump_line(struct nl_object *a, struct nl_dump_params *p) if (sa->curlft.use_time != 0) { use_time = sa->curlft.use_time; - use_time_tm = gmtime (&use_time); + use_time_tm = gmtime_r (&use_time, &tm_buf); strftime (mode, 128, "%Y-%m-%d %H-%M-%S", use_time_tm); } else diff --git a/lib/xfrm/sp.c b/lib/xfrm/sp.c index 0863b12..cf10651 100644 --- a/lib/xfrm/sp.c +++ b/lib/xfrm/sp.c @@ -357,6 +357,7 @@ static void xfrm_sp_dump_line(struct nl_object *a, struct nl_dump_params *p) char dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5]; time_t add_time, use_time; struct tm *add_time_tm, *use_time_tm; + struct tm tm_buf; nl_addr2str(xfrmnl_sel_get_saddr (sp->sel), src, sizeof(src)); nl_addr2str (xfrmnl_sel_get_daddr (sp->sel), dst, sizeof (dst)); @@ -417,7 +418,7 @@ static void xfrm_sp_dump_line(struct nl_object *a, struct nl_dump_params *p) if (sp->curlft.add_time != 0) { add_time = sp->curlft.add_time; - add_time_tm = gmtime (&add_time); + add_time_tm = gmtime_r (&add_time, &tm_buf); strftime (dst, INET6_ADDRSTRLEN+5, "%Y-%m-%d %H-%M-%S", add_time_tm); } else @@ -428,7 +429,7 @@ static void xfrm_sp_dump_line(struct nl_object *a, struct nl_dump_params *p) if (sp->curlft.use_time != 0) { use_time = sp->curlft.use_time; - use_time_tm = gmtime (&use_time); + use_time_tm = gmtime_r (&use_time, &tm_buf); strftime (src, INET6_ADDRSTRLEN+5, "%Y-%m-%d %H-%M-%S", use_time_tm); } else diff --git a/tests/check-direct.c b/tests/check-direct.c index 8cb4932..db1f48d 100644 --- a/tests/check-direct.c +++ b/tests/check-direct.c @@ -41,6 +41,15 @@ START_TEST(static_checks) ck_assert_int_le(i, RTNL_LINK_STATS_MAX); ck_assert_int_eq(i, rtnl_link_str2stat(s)); } + + ck_assert_int_eq(nl_str2ip_proto(""), -NLE_OBJ_NOTFOUND); + ck_assert_int_eq(nl_str2ip_proto("5"), 5); + ck_assert_int_eq(nl_str2ip_proto(" 13 "), -NLE_OBJ_NOTFOUND); + ck_assert_int_eq(nl_str2ip_proto("13"), 13); + ck_assert_int_eq(nl_str2ip_proto("0x13"), 0x13); + ck_assert_int_eq(nl_str2ip_proto("0342"), 0342); + ck_assert_int_eq(nl_str2ip_proto("2147483647"), 2147483647); + ck_assert_int_eq(nl_str2ip_proto("2147483648"), -NLE_OBJ_NOTFOUND); } END_TEST diff --git a/tests/cksuite-all-ematch-tree-clone.c b/tests/cksuite-all-ematch-tree-clone.c index 7f29b85..c48a9dd 100644 --- a/tests/cksuite-all-ematch-tree-clone.c +++ b/tests/cksuite-all-ematch-tree-clone.c @@ -38,12 +38,6 @@ static long long my_pow(long long x, long long y) return ret; } -static long int generate_random(long int max) -{ - srandom(time(NULL) + id); - return (random() % max); -} - static int build_children(struct nl_list_head *parent) { int i, num = 0; @@ -57,7 +51,7 @@ static int build_children(struct nl_list_head *parent) return 0; } - num = generate_random(MAX_CHILDREN + 1); + num = _nltst_rand_u32() % ((unsigned)(MAX_CHILDREN + 1)); for (i = 0; i < num; ++i) { child = rtnl_ematch_alloc(); if (!child) { diff --git a/tests/test-cache-mngr.c b/tests/test-cache-mngr.c index 59ed9e7..cc0e15b 100644 --- a/tests/test-cache-mngr.c +++ b/tests/test-cache-mngr.c @@ -26,9 +26,10 @@ static void print_timestamp(FILE *fp) struct timeval tv; char tshort[40]; struct tm *tm; + struct tm tm_buf; gettimeofday(&tv, NULL); - tm = localtime(&tv.tv_sec); + tm = localtime_r(&tv.tv_sec, &tm_buf); strftime(tshort, sizeof(tshort), "%Y-%m-%dT%H:%M:%S", tm); fprintf(fp, "[%s.%06ld] ", tshort, tv.tv_usec); |