From 20d145efefe2b0ebbca7cf3af5d48c561283dcb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 21 Mar 2018 16:09:08 +0000 Subject: [PATCH 01/10] Replace isc_string_touint64 with strtoull (C99) --- lib/dns/rdata/any_255/tsig_250.c | 2 +- lib/isc/include/isc/string.h | 32 ----------- lib/isc/log.c | 5 +- lib/isc/netscope.c | 4 +- lib/isc/string.c | 76 ------------------------- lib/isc/win32/include/isc/platform.h.in | 12 ++++ lib/isc/win32/libisc.def.in | 2 - lib/isccfg/namedconf.c | 4 +- lib/isccfg/parser.c | 2 +- 9 files changed, 21 insertions(+), 118 deletions(-) diff --git a/lib/dns/rdata/any_255/tsig_250.c b/lib/dns/rdata/any_255/tsig_250.c index 9282c073f1..ed68b24919 100644 --- a/lib/dns/rdata/any_255/tsig_250.c +++ b/lib/dns/rdata/any_255/tsig_250.c @@ -52,7 +52,7 @@ fromtext_any_tsig(ARGS_FROMTEXT) { */ RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string, ISC_FALSE)); - sigtime = isc_string_touint64(DNS_AS_STR(token), &e, 10); + sigtime = strtoull(DNS_AS_STR(token), &e, 10); if (*e != 0) RETTOK(DNS_R_SYNTAX); if ((sigtime >> 48) != 0) diff --git a/lib/isc/include/isc/string.h b/lib/isc/include/isc/string.h index 43e6af63e1..0c77f87b37 100644 --- a/lib/isc/include/isc/string.h +++ b/lib/isc/include/isc/string.h @@ -32,21 +32,6 @@ ISC_LANG_BEGINDECLS -isc_uint64_t -isc_string_touint64(char *source, char **endp, int base); -/*%< - * Convert the string pointed to by 'source' to isc_uint64_t. - * - * On successful conversion 'endp' points to the first character - * after conversion is complete. - * - * 'base': 0 or 2..36 - * - * If base is 0 the base is computed from the string type. - * - * On error 'endp' points to 'source'. - */ - isc_result_t isc_string_copy(char *target, size_t size, const char *source); /* @@ -114,23 +99,6 @@ isc_string_append(char *target, size_t size, const char *source); * is too small. */ -void -isc_string_append_truncate(char *target, size_t size, const char *source); -/* - * Append the string pointed to by 'source' to 'target' which is a - * pointer to a NUL terminated string of at least 'size' bytes. - * - * Requires: - * 'target' is a pointer to a NUL terminated char[] of at - * least 'size' bytes. - * 'size' an integer > 0. - * 'source' == NULL or points to a NUL terminated string. - * - * Ensures: - * 'target' will be a NUL terminated string of no more - * than 'size' bytes (including NUL). - */ - isc_result_t isc_string_printf(char *target, size_t size, const char *format, ...) ISC_FORMAT_PRINTF(3, 4); diff --git a/lib/isc/log.c b/lib/isc/log.c index 224e6653ce..98c0ed45dd 100644 --- a/lib/isc/log.c +++ b/lib/isc/log.c @@ -1280,8 +1280,7 @@ remove_old_tsversions(isc_logfile_t *file, int versions) { dir.entry.name[bnamelen] == '.') { char *ename = &dir.entry.name[bnamelen + 1]; - version = isc_string_touint64(ename, - &digit_end, 10); + version = strtoull(ename, &digit_end, 10); if (*digit_end == '\0') { int i = 0; while (i < versions && @@ -1316,7 +1315,7 @@ remove_old_tsversions(isc_logfile_t *file, int versions) { dir.entry.name[bnamelen] == '.') { char *ename = &dir.entry.name[bnamelen + 1]; - version = isc_string_touint64(ename, &digit_end, 10); + version = strtoull(ename, &digit_end, 10); /* * Remove any backup files that exceed versions. */ diff --git a/lib/isc/netscope.c b/lib/isc/netscope.c index 85a25b6f13..859d05ff68 100644 --- a/lib/isc/netscope.c +++ b/lib/isc/netscope.c @@ -13,6 +13,8 @@ #include +#include + #include #include #include @@ -47,7 +49,7 @@ isc_netscope_pton(int af, char *scopename, void *addr, isc_uint32_t *zoneid) { zone = (isc_uint32_t)ifid; else { #endif - llz = isc_string_touint64(scopename, &ep, 10); + llz = strtoull(scopename, &ep, 10); if (ep == scopename) return (ISC_R_FAILURE); diff --git a/lib/isc/string.c b/lib/isc/string.c index eab78c20b0..a9e5d40aa6 100644 --- a/lib/isc/string.c +++ b/lib/isc/string.c @@ -50,72 +50,6 @@ #include #include -static const char digits[] = "0123456789abcdefghijklmnoprstuvwxyz"; - -isc_uint64_t -isc_string_touint64(char *source, char **end, int base) { - isc_uint64_t tmp; - isc_uint64_t overflow; - char *s = source; - const char *o; - char c; - - if ((base < 0) || (base == 1) || (base > 36)) { - *end = source; - return (0); - } - - while (*s != 0 && isascii(*s&0xff) && isspace(*s&0xff)) - s++; - if (*s == '+' /* || *s == '-' */) - s++; - if (base == 0) { - if (*s == '0' && (*(s+1) == 'X' || *(s+1) == 'x')) { - s += 2; - base = 16; - } else if (*s == '0') - base = 8; - else - base = 10; - } - if (*s == 0) { - *end = source; - return (0); - } - overflow = ~0; - overflow /= base; - tmp = 0; - - while ((c = *s) != 0) { - c = tolower(c&0xff); - /* end ? */ - if ((o = strchr(digits, c)) == NULL) { - *end = s; - return (tmp); - } - /* end ? */ - if ((o - digits) >= base) { - *end = s; - return (tmp); - } - /* overflow ? */ - if (tmp > overflow) { - *end = source; - return (0); - } - tmp *= base; - /* overflow ? */ - if ((tmp + (o - digits)) < tmp) { - *end = source; - return (0); - } - tmp += o - digits; - s++; - } - *end = s; - return (tmp); -} - isc_result_t isc_string_copy(char *target, size_t size, const char *source) { REQUIRE(size > 0U); @@ -154,16 +88,6 @@ isc_string_append(char *target, size_t size, const char *source) { return (ISC_R_SUCCESS); } -void -isc_string_append_truncate(char *target, size_t size, const char *source) { - REQUIRE(size > 0U); - REQUIRE(strlen(target) < size); - - strlcat(target, source, size); - - ENSURE(strlen(target) < size); -} - isc_result_t isc_string_printf(char *target, size_t size, const char *format, ...) { va_list args; diff --git a/lib/isc/win32/include/isc/platform.h.in b/lib/isc/win32/include/isc/platform.h.in index 3da8f37c33..e3f534f7b8 100644 --- a/lib/isc/win32/include/isc/platform.h.in +++ b/lib/isc/win32/include/isc/platform.h.in @@ -20,6 +20,18 @@ #define ISC_PLATFORM_USETHREADS 1 +/* + * Some compatibility cludges + */ + +#if defined(_WIN32) || defined(_WIN64) + +#ifndef strtoull +#define strtoull _strtoui64 +#endif + +#endif + /*** *** Network. ***/ diff --git a/lib/isc/win32/libisc.def.in b/lib/isc/win32/libisc.def.in index 66a44a54be..85beac7e69 100644 --- a/lib/isc/win32/libisc.def.in +++ b/lib/isc/win32/libisc.def.in @@ -646,7 +646,6 @@ isc_stdio_tell isc_stdio_write isc_stdtime_get isc_string_append -isc_string_append_truncate isc_string_copy isc_string_copy_truncate isc_string_printf @@ -656,7 +655,6 @@ isc_string_separate isc_string_strcasestr isc_string_strlcat isc_string_strlcpy -isc_string_touint64 isc_symtab_count isc_symtab_create isc_symtab_define diff --git a/lib/isccfg/namedconf.c b/lib/isccfg/namedconf.c index c3cab304e9..7bd6fce625 100644 --- a/lib/isccfg/namedconf.c +++ b/lib/isccfg/namedconf.c @@ -2552,7 +2552,7 @@ parse_unitstring(char *str, isc_resourcevalue_t *valuep) { isc_uint64_t value; isc_uint64_t unit; - value = isc_string_touint64(str, &endp, 10); + value = strtoull(str, &endp, 10); if (*endp == 0) { *valuep = value; return (ISC_R_SUCCESS); @@ -2628,7 +2628,7 @@ parse_sizeval_percent(cfg_parser_t *pctx, const cfg_type_t *type, goto cleanup; } - percent = isc_string_touint64(TOKEN_STRING(pctx), &endp, 10); + percent = strtoull(TOKEN_STRING(pctx), &endp, 10); if (*endp == '%' && *(endp+1) == 0) { CHECK(cfg_create_obj(pctx, &cfg_type_percentage, &obj)); diff --git a/lib/isccfg/parser.c b/lib/isccfg/parser.c index ef6a92da48..6e840161fb 100644 --- a/lib/isccfg/parser.c +++ b/lib/isccfg/parser.c @@ -752,7 +752,7 @@ cfg_parse_percentage(cfg_parser_t *pctx, const cfg_type_t *type, return (ISC_R_UNEXPECTEDTOKEN); } - percent = isc_string_touint64(TOKEN_STRING(pctx), &endp, 10); + percent = strtoull(TOKEN_STRING(pctx), &endp, 10); if (*endp != '%' || *(endp+1) != 0) { cfg_parser_error(pctx, CFG_LOG_NEAR, "expected percentage"); From ccdb0287e8462c0bcb91798b93f592c4ccc6ddcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 21 Mar 2018 16:20:59 +0000 Subject: [PATCH 02/10] Remove isc_string_copy, isc_string_copy_truncate and isc_string_append. Use strlcpy and strlcat as appropriate instead. --- lib/isc/include/isc/string.h | 67 ------------------------------------ lib/isc/string.c | 38 -------------------- lib/isc/win32/libisc.def.in | 3 -- lib/samples/nsprobe.c | 17 ++++----- 4 files changed, 9 insertions(+), 116 deletions(-) diff --git a/lib/isc/include/isc/string.h b/lib/isc/include/isc/string.h index 0c77f87b37..16987ef750 100644 --- a/lib/isc/include/isc/string.h +++ b/lib/isc/include/isc/string.h @@ -32,73 +32,6 @@ ISC_LANG_BEGINDECLS -isc_result_t -isc_string_copy(char *target, size_t size, const char *source); -/* - * Copy the string pointed to by 'source' to 'target' which is a - * pointer to a string of at least 'size' bytes. - * - * Requires: - * 'target' is a pointer to a char[] of at least 'size' bytes. - * 'size' an integer > 0. - * 'source' == NULL or points to a NUL terminated string. - * - * Ensures: - * If result == ISC_R_SUCCESS - * 'target' will be a NUL terminated string of no more - * than 'size' bytes (including NUL). - * - * If result == ISC_R_NOSPACE - * 'target' is undefined. - * - * Returns: - * ISC_R_SUCCESS -- 'source' was successfully copied to 'target'. - * ISC_R_NOSPACE -- 'source' could not be copied since 'target' - * is too small. - */ - -void -isc_string_copy_truncate(char *target, size_t size, const char *source); -/* - * Copy the string pointed to by 'source' to 'target' which is a - * pointer to a string of at least 'size' bytes. - * - * Requires: - * 'target' is a pointer to a char[] of at least 'size' bytes. - * 'size' an integer > 0. - * 'source' == NULL or points to a NUL terminated string. - * - * Ensures: - * 'target' will be a NUL terminated string of no more - * than 'size' bytes (including NUL). - */ - -isc_result_t -isc_string_append(char *target, size_t size, const char *source); -/* - * Append the string pointed to by 'source' to 'target' which is a - * pointer to a NUL terminated string of at least 'size' bytes. - * - * Requires: - * 'target' is a pointer to a NUL terminated char[] of at - * least 'size' bytes. - * 'size' an integer > 0. - * 'source' == NULL or points to a NUL terminated string. - * - * Ensures: - * If result == ISC_R_SUCCESS - * 'target' will be a NUL terminated string of no more - * than 'size' bytes (including NUL). - * - * If result == ISC_R_NOSPACE - * 'target' is undefined. - * - * Returns: - * ISC_R_SUCCESS -- 'source' was successfully appended to 'target'. - * ISC_R_NOSPACE -- 'source' could not be appended since 'target' - * is too small. - */ - isc_result_t isc_string_printf(char *target, size_t size, const char *format, ...) ISC_FORMAT_PRINTF(3, 4); diff --git a/lib/isc/string.c b/lib/isc/string.c index a9e5d40aa6..5701f1471e 100644 --- a/lib/isc/string.c +++ b/lib/isc/string.c @@ -50,44 +50,6 @@ #include #include -isc_result_t -isc_string_copy(char *target, size_t size, const char *source) { - REQUIRE(size > 0U); - - if (strlcpy(target, source, size) >= size) { - memset(target, ISC_STRING_MAGIC, size); - return (ISC_R_NOSPACE); - } - - ENSURE(strlen(target) < size); - - return (ISC_R_SUCCESS); -} - -void -isc_string_copy_truncate(char *target, size_t size, const char *source) { - REQUIRE(size > 0U); - - strlcpy(target, source, size); - - ENSURE(strlen(target) < size); -} - -isc_result_t -isc_string_append(char *target, size_t size, const char *source) { - REQUIRE(size > 0U); - REQUIRE(strlen(target) < size); - - if (strlcat(target, source, size) >= size) { - memset(target, ISC_STRING_MAGIC, size); - return (ISC_R_NOSPACE); - } - - ENSURE(strlen(target) < size); - - return (ISC_R_SUCCESS); -} - isc_result_t isc_string_printf(char *target, size_t size, const char *format, ...) { va_list args; diff --git a/lib/isc/win32/libisc.def.in b/lib/isc/win32/libisc.def.in index 85beac7e69..ee08d90b42 100644 --- a/lib/isc/win32/libisc.def.in +++ b/lib/isc/win32/libisc.def.in @@ -645,9 +645,6 @@ isc_stdio_sync isc_stdio_tell isc_stdio_write isc_stdtime_get -isc_string_append -isc_string_copy -isc_string_copy_truncate isc_string_printf isc_string_printf_truncate isc_string_regiondup diff --git a/lib/samples/nsprobe.c b/lib/samples/nsprobe.c index 6830c2f615..46088cd8dc 100644 --- a/lib/samples/nsprobe.c +++ b/lib/samples/nsprobe.c @@ -467,17 +467,18 @@ set_nextqname(struct probe_trans *trans) { isc_buffer_t b; char buf[4096]; /* XXX ad-hoc constant, but should be enough */ - if (*trans->qlabel == NULL) + if (*trans->qlabel == NULL) { return (ISC_R_NOMORE); + } - result = isc_string_copy(buf, sizeof(buf), *trans->qlabel); - if (result != ISC_R_SUCCESS) - return (result); - result = isc_string_append(buf, sizeof(buf), trans->domain); - if (result != ISC_R_SUCCESS) - return (result); + if (strlcpy(buf, *trans->qlabel, sizeof(buf)) >= sizeof(buf)) { + return ISC_R_NOSPACE; + } - domainlen = strlen(buf); + if ((domainlen = strlcat(buf, trans->domain, sizeof(buf))) >= sizeof(buf)) { + return ISC_R_NOSPACE; + } + isc_buffer_init(&b, buf, domainlen); isc_buffer_add(&b, domainlen); trans->qname = dns_fixedname_initname(&trans->fixedname); From 9fda5253fdd86d30a85a61b21bb72d8299145842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 21 Mar 2018 16:38:29 +0000 Subject: [PATCH 03/10] Remove isc_string_printf and isc_string_printf_truncate. Replace with simple snprintf() as appropriate. --- bin/nsupdate/nsupdate.c | 22 ++++----- lib/dns/journal.c | 30 ++++++------ lib/dns/ssu.c | 92 ++++++++++++++++++------------------ lib/isc/include/isc/string.h | 44 ----------------- lib/isc/string.c | 36 -------------- lib/isc/win32/libisc.def.in | 2 - 6 files changed, 71 insertions(+), 155 deletions(-) diff --git a/bin/nsupdate/nsupdate.c b/bin/nsupdate/nsupdate.c index e753f74c99..2c671edd40 100644 --- a/bin/nsupdate/nsupdate.c +++ b/bin/nsupdate/nsupdate.c @@ -2910,33 +2910,31 @@ start_gssrequest(dns_name_t *master) { if (realm == NULL) get_ticket_realm(gmctx); - result = isc_string_printf(servicename, sizeof(servicename), - "DNS/%s%s", namestr, realm ? realm : ""); - if (result != ISC_R_SUCCESS) - fatal("isc_string_printf(servicename) failed: %s", - isc_result_totext(result)); + result = snprintf(servicename, sizeof(servicename), "DNS/%s%s", namestr, realm ? realm : ""); + RUNTIME_CHECK(result < sizeof(servicename)); isc_buffer_init(&buf, servicename, strlen(servicename)); isc_buffer_add(&buf, strlen(servicename)); result = dns_name_fromtext(servname, &buf, dns_rootname, 0, NULL); - if (result != ISC_R_SUCCESS) + if (result != ISC_R_SUCCESS) { fatal("dns_name_fromtext(servname) failed: %s", isc_result_totext(result)); + } keyname = dns_fixedname_initname(&fkname); isc_random_get(&val); - result = isc_string_printf(mykeystr, sizeof(mykeystr), "%u.sig-%s", - val, namestr); - if (result != ISC_R_SUCCESS) - fatal("isc_string_printf(mykeystr) failed: %s", - isc_result_totext(result)); + + result = snprintf(mykeystr, sizeof(mykeystr), "%u.sig-%s", val, namestr); + RUNTIME_CHECK(result <= sizeof(mykeystr)); + isc_buffer_init(&buf, mykeystr, strlen(mykeystr)); isc_buffer_add(&buf, strlen(mykeystr)); result = dns_name_fromtext(keyname, &buf, dns_rootname, 0, NULL); - if (result != ISC_R_SUCCESS) + if (result != ISC_R_SUCCESS) { fatal("dns_name_fromtext(keyname) failed: %s", isc_result_totext(result)); + } /* Windows doesn't recognize name compression in the key name. */ keyname->attributes |= DNS_NAMEATTR_NOCOMPRESS; diff --git a/lib/dns/journal.c b/lib/dns/journal.c index 09f665e6c8..d89ea4db1c 100644 --- a/lib/dns/journal.c +++ b/lib/dns/journal.c @@ -705,10 +705,11 @@ dns_journal_open(isc_mem_t *mctx, const char *filename, unsigned int mode, if (namelen > 4U && strcmp(filename + namelen - 4, ".jnl") == 0) namelen -= 4; - result = isc_string_printf(backup, sizeof(backup), "%.*s.jbk", - (int)namelen, filename); - if (result != ISC_R_SUCCESS) - return (result); + result = snprintf(backup, sizeof(backup), "%.*s.jbk", + (int)namelen, filename); + if (result >= sizeof(backup)) { + return ISC_R_NOSPACE; + } result = journal_open(mctx, backup, writable, writable, journalp); } @@ -2100,25 +2101,24 @@ dns_journal_compact(isc_mem_t *mctx, char *filename, isc_uint32_t serial, unsigned int size = 0; isc_result_t result; unsigned int indexend; - char newname[1024]; - char backup[1024]; + char newname[PATH_MAX]; + char backup[PATH_MAX]; isc_boolean_t is_backup = ISC_FALSE; REQUIRE(filename != NULL); namelen = strlen(filename); - if (namelen > 4U && strcmp(filename + namelen - 4, ".jnl") == 0) + if (namelen > 4U && strcmp(filename + namelen - 4, ".jnl") == 0) { namelen -= 4; + } - result = isc_string_printf(newname, sizeof(newname), "%.*s.jnw", - (int)namelen, filename); - if (result != ISC_R_SUCCESS) - return (result); + result = snprintf(newname, sizeof(newname), "%.*s.jnw", + (int)namelen, filename); + RUNTIME_CHECK(result < sizeof(newname)); - result = isc_string_printf(backup, sizeof(backup), "%.*s.jbk", - (int)namelen, filename); - if (result != ISC_R_SUCCESS) - return (result); + result = snprintf(backup, sizeof(backup), "%.*s.jbk", + (int)namelen, filename); + RUNTIME_CHECK(result < sizeof(backup)); result = journal_open(mctx, filename, ISC_FALSE, ISC_FALSE, &j1); if (result == ISC_R_NOTFOUND) { diff --git a/lib/dns/ssu.c b/lib/dns/ssu.c index 3f9cd74961..9ceb4fba90 100644 --- a/lib/dns/ssu.c +++ b/lib/dns/ssu.c @@ -258,37 +258,37 @@ reverse_from_address(dns_name_t *tcpself, const isc_netaddr_t *tcpaddr) { switch (tcpaddr->family) { case AF_INET: l = ntohl(tcpaddr->type.in.s_addr); - result = isc_string_printf(buf, sizeof(buf), - "%lu.%lu.%lu.%lu.IN-ADDR.ARPA.", - (l >> 0) & 0xff, (l >> 8) & 0xff, - (l >> 16) & 0xff, (l >> 24) & 0xff); - RUNTIME_CHECK(result == ISC_R_SUCCESS); + result = snprintf(buf, sizeof(buf), + "%lu.%lu.%lu.%lu.IN-ADDR.ARPA.", + (l >> 0) & 0xff, (l >> 8) & 0xff, + (l >> 16) & 0xff, (l >> 24) & 0xff); + RUNTIME_CHECK(result < sizeof(buf)); break; case AF_INET6: ap = tcpaddr->type.in6.s6_addr; - result = isc_string_printf(buf, sizeof(buf), - "%x.%x.%x.%x.%x.%x.%x.%x." - "%x.%x.%x.%x.%x.%x.%x.%x." - "%x.%x.%x.%x.%x.%x.%x.%x." - "%x.%x.%x.%x.%x.%x.%x.%x." - "IP6.ARPA.", - ap[15] & 0x0f, (ap[15] >> 4) & 0x0f, - ap[14] & 0x0f, (ap[14] >> 4) & 0x0f, - ap[13] & 0x0f, (ap[13] >> 4) & 0x0f, - ap[12] & 0x0f, (ap[12] >> 4) & 0x0f, - ap[11] & 0x0f, (ap[11] >> 4) & 0x0f, - ap[10] & 0x0f, (ap[10] >> 4) & 0x0f, - ap[9] & 0x0f, (ap[9] >> 4) & 0x0f, - ap[8] & 0x0f, (ap[8] >> 4) & 0x0f, - ap[7] & 0x0f, (ap[7] >> 4) & 0x0f, - ap[6] & 0x0f, (ap[6] >> 4) & 0x0f, - ap[5] & 0x0f, (ap[5] >> 4) & 0x0f, - ap[4] & 0x0f, (ap[4] >> 4) & 0x0f, - ap[3] & 0x0f, (ap[3] >> 4) & 0x0f, - ap[2] & 0x0f, (ap[2] >> 4) & 0x0f, - ap[1] & 0x0f, (ap[1] >> 4) & 0x0f, - ap[0] & 0x0f, (ap[0] >> 4) & 0x0f); - RUNTIME_CHECK(result == ISC_R_SUCCESS); + result = snprintf(buf, sizeof(buf), + "%x.%x.%x.%x.%x.%x.%x.%x." + "%x.%x.%x.%x.%x.%x.%x.%x." + "%x.%x.%x.%x.%x.%x.%x.%x." + "%x.%x.%x.%x.%x.%x.%x.%x." + "IP6.ARPA.", + ap[15] & 0x0f, (ap[15] >> 4) & 0x0f, + ap[14] & 0x0f, (ap[14] >> 4) & 0x0f, + ap[13] & 0x0f, (ap[13] >> 4) & 0x0f, + ap[12] & 0x0f, (ap[12] >> 4) & 0x0f, + ap[11] & 0x0f, (ap[11] >> 4) & 0x0f, + ap[10] & 0x0f, (ap[10] >> 4) & 0x0f, + ap[9] & 0x0f, (ap[9] >> 4) & 0x0f, + ap[8] & 0x0f, (ap[8] >> 4) & 0x0f, + ap[7] & 0x0f, (ap[7] >> 4) & 0x0f, + ap[6] & 0x0f, (ap[6] >> 4) & 0x0f, + ap[5] & 0x0f, (ap[5] >> 4) & 0x0f, + ap[4] & 0x0f, (ap[4] >> 4) & 0x0f, + ap[3] & 0x0f, (ap[3] >> 4) & 0x0f, + ap[2] & 0x0f, (ap[2] >> 4) & 0x0f, + ap[1] & 0x0f, (ap[1] >> 4) & 0x0f, + ap[0] & 0x0f, (ap[0] >> 4) & 0x0f); + RUNTIME_CHECK(result < sizeof(buf)); break; default: INSIST(0); @@ -310,27 +310,27 @@ stf_from_address(dns_name_t *stfself, const isc_netaddr_t *tcpaddr) { switch(tcpaddr->family) { case AF_INET: l = ntohl(tcpaddr->type.in.s_addr); - result = isc_string_printf(buf, sizeof(buf), - "%lx.%lx.%lx.%lx.%lx.%lx.%lx.%lx" - "2.0.0.2.IP6.ARPA.", - l & 0xf, (l >> 4) & 0xf, - (l >> 8) & 0xf, (l >> 12) & 0xf, - (l >> 16) & 0xf, (l >> 20) & 0xf, - (l >> 24) & 0xf, (l >> 28) & 0xf); - RUNTIME_CHECK(result == ISC_R_SUCCESS); + result = snprintf(buf, sizeof(buf), + "%lx.%lx.%lx.%lx.%lx.%lx.%lx.%lx" + "2.0.0.2.IP6.ARPA.", + l & 0xf, (l >> 4) & 0xf, + (l >> 8) & 0xf, (l >> 12) & 0xf, + (l >> 16) & 0xf, (l >> 20) & 0xf, + (l >> 24) & 0xf, (l >> 28) & 0xf); + RUNTIME_CHECK(result < sizeof(buf)); break; case AF_INET6: ap = tcpaddr->type.in6.s6_addr; - result = isc_string_printf(buf, sizeof(buf), - "%x.%x.%x.%x.%x.%x.%x.%x." - "%x.%x.%x.%x.IP6.ARPA.", - ap[5] & 0x0f, (ap[5] >> 4) & 0x0f, - ap[4] & 0x0f, (ap[4] >> 4) & 0x0f, - ap[3] & 0x0f, (ap[3] >> 4) & 0x0f, - ap[2] & 0x0f, (ap[2] >> 4) & 0x0f, - ap[1] & 0x0f, (ap[1] >> 4) & 0x0f, - ap[0] & 0x0f, (ap[0] >> 4) & 0x0f); - RUNTIME_CHECK(result == ISC_R_SUCCESS); + result = snprintf(buf, sizeof(buf), + "%x.%x.%x.%x.%x.%x.%x.%x." + "%x.%x.%x.%x.IP6.ARPA.", + ap[5] & 0x0f, (ap[5] >> 4) & 0x0f, + ap[4] & 0x0f, (ap[4] >> 4) & 0x0f, + ap[3] & 0x0f, (ap[3] >> 4) & 0x0f, + ap[2] & 0x0f, (ap[2] >> 4) & 0x0f, + ap[1] & 0x0f, (ap[1] >> 4) & 0x0f, + ap[0] & 0x0f, (ap[0] >> 4) & 0x0f); + RUNTIME_CHECK(result < sizeof(buf)); break; default: INSIST(0); diff --git a/lib/isc/include/isc/string.h b/lib/isc/include/isc/string.h index 16987ef750..eb9376039a 100644 --- a/lib/isc/include/isc/string.h +++ b/lib/isc/include/isc/string.h @@ -32,50 +32,6 @@ ISC_LANG_BEGINDECLS -isc_result_t -isc_string_printf(char *target, size_t size, const char *format, ...) - ISC_FORMAT_PRINTF(3, 4); -/* - * Print 'format' to 'target' which is a pointer to a string of at least - * 'size' bytes. - * - * Requires: - * 'target' is a pointer to a char[] of at least 'size' bytes. - * 'size' an integer > 0. - * 'format' == NULL or points to a NUL terminated string. - * - * Ensures: - * If result == ISC_R_SUCCESS - * 'target' will be a NUL terminated string of no more - * than 'size' bytes (including NUL). - * - * If result == ISC_R_NOSPACE - * 'target' is undefined. - * - * Returns: - * ISC_R_SUCCESS -- 'format' was successfully printed to 'target'. - * ISC_R_NOSPACE -- 'format' could not be printed to 'target' since it - * is too small. - */ - -void -isc_string_printf_truncate(char *target, size_t size, const char *format, ...) - ISC_FORMAT_PRINTF(3, 4); -/* - * Print 'format' to 'target' which is a pointer to a string of at least - * 'size' bytes. - * - * Requires: - * 'target' is a pointer to a char[] of at least 'size' bytes. - * 'size' an integer > 0. - * 'format' == NULL or points to a NUL terminated string. - * - * Ensures: - * 'target' will be a NUL terminated string of no more - * than 'size' bytes (including NUL). - */ - - char * isc_string_regiondup(isc_mem_t *mctx, const isc_region_t *source); /* diff --git a/lib/isc/string.c b/lib/isc/string.c index 5701f1471e..ddd9c8ca3f 100644 --- a/lib/isc/string.c +++ b/lib/isc/string.c @@ -50,42 +50,6 @@ #include #include -isc_result_t -isc_string_printf(char *target, size_t size, const char *format, ...) { - va_list args; - size_t n; - - REQUIRE(size > 0U); - - va_start(args, format); - n = vsnprintf(target, size, format, args); - va_end(args); - - if (n >= size) { - memset(target, ISC_STRING_MAGIC, size); - return (ISC_R_NOSPACE); - } - - ENSURE(strlen(target) < size); - - return (ISC_R_SUCCESS); -} - -void -isc_string_printf_truncate(char *target, size_t size, const char *format, ...) -{ - va_list args; - - REQUIRE(size > 0U); - - va_start(args, format); - /* check return code? */ - (void)vsnprintf(target, size, format, args); - va_end(args); - - ENSURE(strlen(target) < size); -} - char * isc_string_regiondup(isc_mem_t *mctx, const isc_region_t *source) { char *target; diff --git a/lib/isc/win32/libisc.def.in b/lib/isc/win32/libisc.def.in index ee08d90b42..d7dc4101e0 100644 --- a/lib/isc/win32/libisc.def.in +++ b/lib/isc/win32/libisc.def.in @@ -645,8 +645,6 @@ isc_stdio_sync isc_stdio_tell isc_stdio_write isc_stdtime_get -isc_string_printf -isc_string_printf_truncate isc_string_regiondup isc_string_separate isc_string_strcasestr From b9552250cb35e82122605e1530d957087a1f8782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 21 Mar 2018 16:40:42 +0000 Subject: [PATCH 04/10] Remove unused isc_string_regiondup function. --- lib/isc/include/isc/string.h | 18 ------------------ lib/isc/string.c | 16 ---------------- lib/isc/win32/libisc.def.in | 1 - 3 files changed, 35 deletions(-) diff --git a/lib/isc/include/isc/string.h b/lib/isc/include/isc/string.h index eb9376039a..c32abb3aa4 100644 --- a/lib/isc/include/isc/string.h +++ b/lib/isc/include/isc/string.h @@ -32,24 +32,6 @@ ISC_LANG_BEGINDECLS -char * -isc_string_regiondup(isc_mem_t *mctx, const isc_region_t *source); -/* - * Copy the region pointed to by r to a NUL terminated string - * allocated from the memory context pointed to by mctx. - * - * The result should be deallocated using isc_mem_free() - * - * Requires: - * 'mctx' is a point to a valid memory context. - * 'source' is a pointer to a valid region. - * - * Returns: - * a pointer to a NUL terminated string or - * NULL if memory for the copy could not be allocated - * - */ - char * isc_string_separate(char **stringp, const char *delim); diff --git a/lib/isc/string.c b/lib/isc/string.c index ddd9c8ca3f..01ceeb1775 100644 --- a/lib/isc/string.c +++ b/lib/isc/string.c @@ -50,22 +50,6 @@ #include #include -char * -isc_string_regiondup(isc_mem_t *mctx, const isc_region_t *source) { - char *target; - - REQUIRE(mctx != NULL); - REQUIRE(source != NULL); - - target = (char *) isc_mem_allocate(mctx, source->length + 1); - if (target != NULL) { - memmove(source->base, target, source->length); - target[source->length] = '\0'; - } - - return (target); -} - char * isc_string_separate(char **stringp, const char *delim) { char *string = *stringp; diff --git a/lib/isc/win32/libisc.def.in b/lib/isc/win32/libisc.def.in index d7dc4101e0..a5f1960e95 100644 --- a/lib/isc/win32/libisc.def.in +++ b/lib/isc/win32/libisc.def.in @@ -645,7 +645,6 @@ isc_stdio_sync isc_stdio_tell isc_stdio_write isc_stdtime_get -isc_string_regiondup isc_string_separate isc_string_strcasestr isc_string_strlcat From 921d05ddcf1aed657227d74763afb006a6b24f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 21 Mar 2018 21:08:29 +0000 Subject: [PATCH 05/10] Replace usage of strsep with POSIX strtok_r() --- bin/delv/delv.c | 26 ++------ bin/dig/dig.c | 98 ++++++++++++---------------- bin/dig/dighost.c | 12 ---- bin/dig/include/dig/dig.h | 3 - bin/dig/nslookup.c | 8 +-- bin/named/unix/os.c | 26 ++------ bin/tools/mdig.c | 40 ++++-------- configure | 31 --------- configure.in | 12 ---- contrib/dlz/bin/dlzbdb/dlzbdb.c | 85 +++++------------------- contrib/dlz/drivers/sdlz_helper.c | 4 +- contrib/dlz/modules/common/dlz_dbi.c | 16 +++-- lib/irs/getaddrinfo.c | 41 +++--------- lib/isc/include/isc/platform.h.in | 8 --- lib/isc/include/isc/string.h | 7 -- lib/isc/string.c | 21 ------ lib/isc/win32/libisc.def.in | 1 - 17 files changed, 109 insertions(+), 330 deletions(-) diff --git a/bin/delv/delv.c b/bin/delv/delv.c index bf1fa104e2..1bcc9e7e3b 100644 --- a/bin/delv/delv.c +++ b/bin/delv/delv.c @@ -943,18 +943,6 @@ cleanup: return (result); } -static char * -next_token(char **stringp, const char *delim) { - char *res; - - do { - res = strsep(stringp, delim); - if (res == NULL) - break; - } while (*res == '\0'); - return (res); -} - static isc_result_t parse_uint(isc_uint32_t *uip, const char *value, isc_uint32_t max, const char *desc) { @@ -974,23 +962,21 @@ parse_uint(isc_uint32_t *uip, const char *value, isc_uint32_t max, static void plus_option(char *option) { isc_result_t result; - char option_store[256]; - char *cmd, *value, *ptr; + char *cmd, *value, *last; isc_boolean_t state = ISC_TRUE; - strlcpy(option_store, option, sizeof(option_store)); - ptr = option_store; - cmd = next_token(&ptr,"="); + cmd = strtok_r(option, "=", &last); if (cmd == NULL) { - printf(";; Invalid option %s\n", option_store); + printf(";; Invalid option %s\n", option); return; - } - value = ptr; + } if (strncasecmp(cmd, "no", 2)==0) { cmd += 2; state = ISC_FALSE; } + value = strtok_r(NULL, "\0", &last); + #define FULLCHECK(A) \ do { \ size_t _l = strlen(cmd); \ diff --git a/bin/dig/dig.c b/bin/dig/dig.c index 71a00939ac..dcd11f14fc 100644 --- a/bin/dig/dig.c +++ b/bin/dig/dig.c @@ -729,28 +729,25 @@ printgreeting(int argc, char **argv, dig_lookup_t *lookup) { */ static void -plus_option(const char *option, isc_boolean_t is_batchfile, +plus_option(char *option, isc_boolean_t is_batchfile, dig_lookup_t *lookup) { isc_result_t result; - char option_store[256]; - char *cmd, *value, *ptr, *code; + char *cmd, *value, *last, *code; isc_uint32_t num; isc_boolean_t state = ISC_TRUE; size_t n; - strlcpy(option_store, option, sizeof(option_store)); - ptr = option_store; - cmd = next_token(&ptr, "="); - if (cmd == NULL) { - printf(";; Invalid option %s\n", option_store); + if ((cmd = strtok_r(option, "=", &last)) == NULL) { + printf(";; Invalid option %s\n", option); return; } - value = ptr; if (strncasecmp(cmd, "no", 2)==0) { cmd += 2; state = ISC_FALSE; } + /* parse the rest of the string */ + value = strtok_r(NULL, "", &last); #define FULLCHECK(A) \ do { \ @@ -1006,8 +1003,10 @@ plus_option(const char *option, isc_boolean_t is_batchfile, "specified"); goto exit_or_usage; } - code = next_token(&value, ":"); - save_opt(lookup, code, value); + char *extra; + code = strtok_r(value, ":", &last); + extra = strtok_r(NULL, "\0", &last); + save_opt(lookup, code, extra); break; default: goto invalid_option; @@ -1524,7 +1523,7 @@ dash_option(char *option, char *next, dig_lookup_t **lookup, isc_boolean_t config_only, int argc, char **argv, isc_boolean_t *firstarg) { - char opt, *value, *ptr, *ptr2, *ptr3; + char opt, *value, *ptr, *ptr2, *ptr3, *last; isc_result_t result; isc_boolean_t value_from_next; isc_textregion_t tr; @@ -1738,15 +1737,13 @@ dash_option(char *option, char *next, dig_lookup_t **lookup, value); return (value_from_next); case 'y': - ptr = next_token(&value, ":"); /* hmac type or name */ - if (ptr == NULL) { + if ((ptr = strtok_r(value, ":", &last)) == NULL) { usage(); } - ptr2 = next_token(&value, ":"); /* name or secret */ - if (ptr2 == NULL) + if ((ptr2 = strtok_r(NULL, ":", &last)) == NULL) { /* name or secret */ usage(); - ptr3 = next_token(&value, ":"); /* secret or NULL */ - if (ptr3 != NULL) { + } + if ((ptr3 = strtok_r(NULL, ":", &last)) != NULL) { /* secret or NULL */ parse_hmac(ptr); ptr = ptr2; ptr2 = ptr3; @@ -1758,6 +1755,7 @@ dash_option(char *option, char *next, dig_lookup_t **lookup, #endif digestbits = 0; } + /* XXXONDREJ: FIXME */ strlcpy(keynametext, ptr, sizeof(keynametext)); strlcpy(keysecret, ptr2, sizeof(keysecret)); return (value_from_next); @@ -1859,10 +1857,9 @@ parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only, char **rv; #ifndef NOPOSIX char *homedir; - char rcfile[256]; + char rcfile[PATH_MAX]; #endif - char *input; - int i; + char *last; isc_boolean_t need_clone = ISC_TRUE; /* @@ -1892,32 +1889,23 @@ parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only, homedir = getenv("HOME"); if (homedir != NULL) { unsigned int n; - n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc", - homedir); - if (n < sizeof(rcfile)) + n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc", homedir); + if (n < sizeof(rcfile)) { batchfp = fopen(rcfile, "r"); + } } if (batchfp != NULL) { - while (fgets(batchline, sizeof(batchline), - batchfp) != 0) { + while (fgets(batchline, sizeof(batchline), batchfp) != 0) { debug("config line %s", batchline); - bargc = 1; - input = batchline; - bargv[bargc] = next_token(&input, " \t\r\n"); - while ((bargc < 62) && (bargv[bargc] != NULL)) { - bargc++; - bargv[bargc] = - next_token(&input, " \t\r\n"); + for (bargc = 1, bargv[bargc] = strtok_r(batchline, " \t\r\n", &last); + bargc < 62 && bargv[bargc]; + bargv[++bargc] = strtok_r(NULL, " \t\r\n", &last)) + { + debug(".digrc argv %d: %s", bargc, bargv[bargc]); } - bargv[0] = argv[0]; argv0 = argv[0]; - - for(i = 0; i < bargc; i++) - debug(".digrc argv %d: %s", - i, bargv[i]); - parse_args(ISC_TRUE, ISC_TRUE, bargc, - (char **)bargv); + parse_args(ISC_TRUE, ISC_TRUE, bargc, (char **)bargv); } fclose(batchfp); } @@ -1928,8 +1916,9 @@ parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only, /* Processing '-f batchfile'. */ lookup = clone_lookup(default_lookup, ISC_TRUE); need_clone = ISC_FALSE; - } else + } else { lookup = default_lookup; + } rc = argc; rv = argv; @@ -2101,18 +2090,15 @@ parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only, if (batchline[0] == '\r' || batchline[0] == '\n' || batchline[0] == '#' || batchline[0] == ';') goto next_line; - input = batchline; - bargv[bargc] = next_token(&input, " \t\r\n"); - while ((bargc < 14) && (bargv[bargc] != NULL)) { - bargc++; - bargv[bargc] = next_token(&input, " \t\r\n"); + for (bargc = 1, bargv[bargc] = strtok_r(batchline, " \t\r\n", &last); + (bargc < 14) && bargv[bargc]; + bargc++, bargv[bargc] = strtok_r(NULL, " \t\r\n", &last)) { + debug("batch argv %d: %s", bargc, bargv[bargc]); } bargv[0] = argv[0]; argv0 = argv[0]; - for(i = 0; i < bargc; i++) - debug("batch argv %d: %s", i, bargv[i]); parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv); return; } @@ -2151,8 +2137,6 @@ query_finished(void) { char batchline[MXNAME]; int bargc; char *bargv[16]; - char *input; - int i; if (batchname == NULL) { isc_app_shutdown(); @@ -2169,19 +2153,17 @@ query_finished(void) { } if (fgets(batchline, sizeof(batchline), batchfp) != 0) { + char *last; debug("batch line %s", batchline); - bargc = 1; - input = batchline; - bargv[bargc] = next_token(&input, " \t\r\n"); - while ((bargc < 14) && (bargv[bargc] != NULL)) { - bargc++; - bargv[bargc] = next_token(&input, " \t\r\n"); + for (bargc = 1, bargv[bargc] = strtok_r(batchline, " \t\r\n", &last); + bargc < 14 && bargv[bargc]; + bargc++, bargv[bargc] = strtok_r(NULL, " \t\r\n", &last)) + { + debug("batch argv %d: %s", bargc, bargv[bargc]); } bargv[0] = argv0; - for(i = 0; i < bargc; i++) - debug("batch argv %d: %s", i, bargv[i]); parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv); start_lookup(); } else { diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c index 85f1b01507..172b94bf13 100644 --- a/bin/dig/dighost.c +++ b/bin/dig/dighost.c @@ -242,18 +242,6 @@ check_next_lookup(dig_lookup_t *lookup); static isc_boolean_t next_origin(dig_lookup_t *oldlookup); -char * -next_token(char **stringp, const char *delim) { - char *res; - - do { - res = strsep(stringp, delim); - if (res == NULL) - break; - } while (*res == '\0'); - return (res); -} - static int count_dots(char *string) { char *s; diff --git a/bin/dig/include/dig/dig.h b/bin/dig/include/dig/dig.h index 6cf1683f00..0b6fc460d0 100644 --- a/bin/dig/include/dig/dig.h +++ b/bin/dig/include/dig/dig.h @@ -369,9 +369,6 @@ destroy_libs(void); void set_search_domain(char *domain); -char * -next_token(char **stringp, const char *delim); - /* * Routines to be defined in dig.c, host.c, and nslookup.c. and * then assigned to the appropriate function pointer diff --git a/bin/dig/nslookup.c b/bin/dig/nslookup.c index ebb850209c..16842a239b 100644 --- a/bin/dig/nslookup.c +++ b/bin/dig/nslookup.c @@ -813,12 +813,12 @@ addlookup(char *opt) { static void do_next_command(char *input) { - char *ptr, *arg; + char *ptr, *arg, *last; - ptr = next_token(&input, " \t\r\n"); - if (ptr == NULL) + if ((ptr = strtok_r(input, " \t\r\n", &last)) == NULL) { return; - arg = next_token(&input, " \t\r\n"); + } + arg = strtok_r(NULL, " \t\r\n", &last); if ((strcasecmp(ptr, "set") == 0) && (arg != NULL)) setoption(arg); diff --git a/bin/named/unix/os.c b/bin/named/unix/os.c index d3b2b2215e..9448c5b997 100644 --- a/bin/named/unix/os.c +++ b/bin/named/unix/os.c @@ -1003,36 +1003,24 @@ named_os_gethostname(char *buf, size_t len) { return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE); } -static char * -next_token(char **stringp, const char *delim) { - char *res; - - do { - res = strsep(stringp, delim); - if (res == NULL) - break; - } while (*res == '\0'); - return (res); -} - void named_os_shutdownmsg(char *command, isc_buffer_t *text) { - char *input, *ptr; + char *last, *ptr; pid_t pid; - input = command; /* Skip the command name. */ - ptr = next_token(&input, " \t"); - if (ptr == NULL) + if ((ptr = strtok_r(command, " \t", &last)) == NULL) { return; + } - ptr = next_token(&input, " \t"); - if (ptr == NULL) + if ((ptr = strtok_r(NULL, " \t", &last)) == NULL) { return; + } - if (strcmp(ptr, "-p") != 0) + if (strcmp(ptr, "-p") != 0) { return; + } #ifdef HAVE_LINUXTHREADS pid = mainpid; diff --git a/bin/tools/mdig.c b/bin/tools/mdig.c index 5cac77ef64..e9e0c52ede 100644 --- a/bin/tools/mdig.c +++ b/bin/tools/mdig.c @@ -795,18 +795,6 @@ help(void) { stdout); } -static char * -next_token(char **stringp, const char *delim) { - char *res; - - do { - res = strsep(stringp, delim); - if (res == NULL) - break; - } while (*res == '\0'); - return (res); -} - ISC_PLATFORM_NORETURN_PRE static void fatal(const char *format, ...) ISC_FORMAT_PRINTF(1, 2) ISC_PLATFORM_NORETURN_POST; @@ -1037,24 +1025,21 @@ static void plus_option(char *option, struct query *query, isc_boolean_t global) { isc_result_t result; - char option_store[256]; - char *cmd, *value, *ptr, *code; + char *cmd, *value, *last, *code; isc_uint32_t num; isc_boolean_t state = ISC_TRUE; size_t n; - strlcpy(option_store, option, sizeof(option_store)); - ptr = option_store; - cmd = next_token(&ptr, "="); - if (cmd == NULL) { - printf(";; Invalid option %s\n", option_store); + if ((cmd = strtok_r(option, "=", &last)) == NULL) { + printf(";; Invalid option %s\n", option); return; } - value = ptr; if (strncasecmp(cmd, "no", 2) == 0) { cmd += 2; state = ISC_FALSE; } + /* parse the rest of the string */ + value = strtok_r(NULL, "", &last); #define FULLCHECK(A) \ do { \ @@ -1279,7 +1264,7 @@ plus_option(char *option, struct query *query, isc_boolean_t global) fatal("ednsopt no " "code point " "specified"); - code = next_token(&value, ":"); + code = strtok_r(value, ":", &last); save_opt(query, code, value); break; default: @@ -1755,7 +1740,6 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) char *bargv[64]; int rc; char **rv; - char *input; isc_boolean_t global = ISC_TRUE; /* @@ -1868,15 +1852,15 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) fatal("couldn't open batch file '%s'", batchname); } while (fgets(batchline, sizeof(batchline), batchfp) != 0) { - bargc = 1; + char *last; if (batchline[0] == '\r' || batchline[0] == '\n' || batchline[0] == '#' || batchline[0] == ';') continue; - input = batchline; - bargv[bargc] = next_token(&input, " \t\r\n"); - while ((bargc < 14) && (bargv[bargc] != NULL)) { - bargc++; - bargv[bargc] = next_token(&input, " \t\r\n"); + for (bargc = 1, bargv[bargc] = strtok_r(batchline, " \t\r\n", &last); + (bargc < 14) && bargv[bargc]; + bargc++, bargv[bargc] = strtok_r(NULL, " \t\r\n", &last)) + { + /* empty body */ } bargv[0] = argv[0]; diff --git a/configure b/configure index aac711428c..ca1af58b3d 100755 --- a/configure +++ b/configure @@ -747,7 +747,6 @@ ISC_PLATFORM_NEEDSTRLCPY GENRANDOMLIB ISC_PLATFORM_NEEDSTRTOUL ISC_PLATFORM_NEEDMEMMOVE -ISC_PLATFORM_NEEDSTRSEP ISC_IRS_GETNAMEINFOSOCKLEN ISC_IRS_NEEDADDRINFO ISC_PLATFORM_HAVETFO @@ -19429,36 +19428,6 @@ esac # Check for some other useful functions that are not ever-present. # -# We test for strsep() using AC_TRY_LINK instead of AC_CHECK_FUNC -# because AIX 4.3.3 with patches for bos.adt.include to version 4.3.3.77 -# reportedly defines strsep() without declaring it in when -# -D_LINUX_SOURCE_COMPAT is not defined [RT #2190], and -# AC_CHECK_FUNC() incorrectly succeeds because it declares -# the function itself. -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for correctly declared strsep()" >&5 -$as_echo_n "checking for correctly declared strsep()... " >&6; } -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include -int -main () -{ -char *sp; char *foo = strsep(&sp, "."); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; }; ISC_PLATFORM_NEEDSTRSEP="#undef ISC_PLATFORM_NEEDSTRSEP" -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; }; ISC_PLATFORM_NEEDSTRSEP="#define ISC_PLATFORM_NEEDSTRSEP 1" -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext - - ac_fn_c_check_func "$LINENO" "memmove" "ac_cv_func_memmove" if test "x$ac_cv_func_memmove" = xyes; then : ISC_PLATFORM_NEEDMEMMOVE="#undef ISC_PLATFORM_NEEDMEMMOVE" diff --git a/configure.in b/configure.in index 9e8a88dfd8..87136d0e6c 100644 --- a/configure.in +++ b/configure.in @@ -3492,18 +3492,6 @@ esac # Check for some other useful functions that are not ever-present. # -# We test for strsep() using AC_TRY_LINK instead of AC_CHECK_FUNC -# because AIX 4.3.3 with patches for bos.adt.include to version 4.3.3.77 -# reportedly defines strsep() without declaring it in when -# -D_LINUX_SOURCE_COMPAT is not defined [RT #2190], and -# AC_CHECK_FUNC() incorrectly succeeds because it declares -# the function itself. -AC_MSG_CHECKING(for correctly declared strsep()) -AC_TRY_LINK([#include ], [char *sp; char *foo = strsep(&sp, ".");], - [AC_MSG_RESULT(yes); ISC_PLATFORM_NEEDSTRSEP="#undef ISC_PLATFORM_NEEDSTRSEP"], - [AC_MSG_RESULT(no); ISC_PLATFORM_NEEDSTRSEP="#define ISC_PLATFORM_NEEDSTRSEP 1"]) -AC_SUBST(ISC_PLATFORM_NEEDSTRSEP) - AC_CHECK_FUNC(memmove, [ISC_PLATFORM_NEEDMEMMOVE="#undef ISC_PLATFORM_NEEDMEMMOVE"], [ISC_PLATFORM_NEEDMEMMOVE="#define ISC_PLATFORM_NEEDMEMMOVE 1"]) diff --git a/contrib/dlz/bin/dlzbdb/dlzbdb.c b/contrib/dlz/bin/dlzbdb/dlzbdb.c index f4aba743e4..b57f9e8e52 100644 --- a/contrib/dlz/bin/dlzbdb/dlzbdb.c +++ b/contrib/dlz/bin/dlzbdb/dlzbdb.c @@ -286,47 +286,24 @@ quit(1); int getzone(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey) { - char *tmp; - char *left; - char *right; - int result=0; + char *token, *last; UNUSED(dbp); UNUSED(pkey); - /* Allocate memory to use in parsing the string */ - tmp = right = malloc(pdata->size + 1); - - /* verify memory was allocated */ - if (right == NULL) { - result = BDBparseErr; - goto getzone_cleanup; - } - - /* copy data string into newly allocated memory */ - strncpy(right, pdata->data, pdata->size); - right[pdata->size] = '\0'; - - /* split string at the first space */ - left = isc_string_separate(&right, " "); + if ((token = strtok_r(pdata->data, " ", &last)) == NULL) { + return BDBparseErr; + } /* copy string for "zone" secondary index */ - skey->data = strdup(left); - if (skey->data == NULL) { - result = BDBparseErr; - goto getzone_cleanup; + if ((skey->data = strdup(token)) == NULL) { + return BDBparseErr; } /* set required values for BDB */ skey->size = strlen(skey->data); skey->flags = DB_DBT_APPMALLOC; - getzone_cleanup: - - /* cleanup memory */ - if (tmp != NULL) - free(tmp); - - return result; + return 0; } /*% @@ -335,56 +312,30 @@ getzone(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey) { int gethost(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey) { - char *tmp; - char *left; - char *right; - int result=0; + char *token, *last; UNUSED(dbp); UNUSED(pkey); - /* allocate memory to use in parsing the string */ - tmp = right = malloc(pdata->size + 1); - - /* verify memory was allocated */ - if (tmp == NULL) { - result = BDBparseErr; - goto gethost_cleanup; - } - - /* copy data string into newly allocated memory */ - strncpy(right, pdata->data, pdata->size); - right[pdata->size] = '\0'; - - /* we don't care about left string. */ - /* memory of left string will be freed when tmp is freed. */ - isc_string_separate(&right, " "); - - /* verify right still has some characters left */ - if (right == NULL) { - result = BDBparseErr; - goto gethost_cleanup; + /* we don't care about first token. */ + if ((token = strtok_r(right, " ", &last)) == NULL) { + return BDBparseErr; } /* get "host" from data string */ - left = isc_string_separate(&right, " "); + if ((token = strtok_r(NULL, " ", &last)) == NULL) { + return BDBparseErr; + } + /* copy string for "host" secondary index */ - skey->data = strdup(left); - if (skey->data == NULL) { - result = BDBparseErr; - goto gethost_cleanup; + if ((skey->data = strdup(token)) == NULL) { + return BDBparseErr; } /* set required values for BDB */ skey->size = strlen(skey->data); skey->flags = DB_DBT_APPMALLOC; - gethost_cleanup: - - /* cleanup memory */ - if (tmp != NULL) - free(tmp); - - return result; + return 0; } /*% diff --git a/contrib/dlz/drivers/sdlz_helper.c b/contrib/dlz/drivers/sdlz_helper.c index 67f364e901..46bb61dcb7 100644 --- a/contrib/dlz/drivers/sdlz_helper.c +++ b/contrib/dlz/drivers/sdlz_helper.c @@ -158,9 +158,9 @@ build_querylist(isc_mem_t *mctx, const char *query_str, char **zone, * split string at the first "$". set query segment to * left portion */ + char *last = NULL; tseg->sql = isc_mem_strdup(mctx, - isc_string_separate(&right_str, - "$")); + strtok_r(right_str, "$", &last)); if (tseg->sql == NULL) { /* no memory, clean everything up. */ result = ISC_R_NOMEMORY; diff --git a/contrib/dlz/modules/common/dlz_dbi.c b/contrib/dlz/modules/common/dlz_dbi.c index 01d98218dc..488baf51b6 100644 --- a/contrib/dlz/modules/common/dlz_dbi.c +++ b/contrib/dlz/modules/common/dlz_dbi.c @@ -132,14 +132,16 @@ build_querylist(const char *query_str, char **zone, char **record, } /* loop through the string and chop it up */ - while (right_str != NULL) { + for (token = strtok_r(right_str, "$", &temp_str); + token; + token = strtok_r(NULL, "$", &temp_str)) + { /* allocate memory for tseg */ tseg = calloc(1, sizeof(query_segment_t)); if (tseg == NULL) { /* no memory, clean everything up. */ result = ISC_R_NOMEMORY; goto cleanup; } - tseg->cmd = NULL; tseg->direct = ISC_FALSE; /* initialize the query segment link */ DLZ_LINK_INIT(tseg, link); @@ -150,7 +152,7 @@ build_querylist(const char *query_str, char **zone, char **record, * split string at the first "$". set query segment to * left portion */ - tseg->cmd = strdup(strsep(&right_str, "$")); + tseg->cmd = strdup(token); if (tseg->cmd == NULL) { /* no memory, clean everything up. */ result = ISC_R_NOMEMORY; @@ -203,7 +205,8 @@ build_querylist(const char *query_str, char **zone, char **record, } /* we don't need temp_str any more */ - free(temp_str); + free(right_str); + right_str = NULL; /* * add checks later to verify zone and record are found if * necessary. @@ -246,8 +249,9 @@ build_querylist(const char *query_str, char **zone, char **record, cleanup: /* get rid of temp_str */ - if (temp_str != NULL) - free(temp_str); + if (right_str != NULL) { + free(right_str); + } flag_fail: /* get rid of what was build of the query list */ diff --git a/lib/irs/getaddrinfo.c b/lib/irs/getaddrinfo.c index 74fd80f75d..2aed01c315 100644 --- a/lib/irs/getaddrinfo.c +++ b/lib/irs/getaddrinfo.c @@ -1051,29 +1051,6 @@ resolve_name(int family, const char *hostname, int flags, return (error); } -static char * -irs_strsep(char **stringp, const char *delim) { - char *string = *stringp; - char *s; - const char *d; - char sc, dc; - - if (string == NULL) - return (NULL); - - for (s = string; *s != '\0'; s++) { - sc = *s; - for (d = delim; (dc = *d) != '\0'; d++) - if (sc == dc) { - *s++ = '\0'; - *stringp = s; - return (string); - } - } - *stringp = NULL; - return (string); -} - static void set_order(int family, int (**net_order)(const char *, int, struct addrinfo **, int, int)) @@ -1093,19 +1070,21 @@ set_order(int family, int (**net_order)(const char *, int, struct addrinfo **, } else { order = getenv("NET_ORDER"); found = 0; - while (order != NULL) { - /* - * We ignore any unknown names. - */ - tok = irs_strsep(&order, ":"); + char *last; + for (tok = strtok_r(order, ":", &last); + tok; + tok = strtok_r(NULL, ":", &last)) + { if (strcasecmp(tok, "inet6") == 0) { - if ((found & FOUND_IPV6) == 0) + if ((found & FOUND_IPV6) == 0) { *net_order++ = add_ipv6; + } found |= FOUND_IPV6; } else if (strcasecmp(tok, "inet") == 0 || - strcasecmp(tok, "inet4") == 0) { - if ((found & FOUND_IPV4) == 0) + strcasecmp(tok, "inet4") == 0) { + if ((found & FOUND_IPV4) == 0) { *net_order++ = add_ipv4; + } found |= FOUND_IPV4; } } diff --git a/lib/isc/include/isc/platform.h.in b/lib/isc/include/isc/platform.h.in index 41613ec33c..c511fa99f7 100644 --- a/lib/isc/include/isc/platform.h.in +++ b/lib/isc/include/isc/platform.h.in @@ -183,14 +183,6 @@ */ @ISC_PLATFORM_QUADFORMAT@ -/*** - *** String functions. - ***/ -/* - * If the system needs strsep(), ISC_PLATFORM_NEEDSTRSEP will be defined. - */ -@ISC_PLATFORM_NEEDSTRSEP@ - /* * If the system needs strlcpy(), ISC_PLATFORM_NEEDSTRLCPY will be defined. */ diff --git a/lib/isc/include/isc/string.h b/lib/isc/include/isc/string.h index c32abb3aa4..67ed24e615 100644 --- a/lib/isc/include/isc/string.h +++ b/lib/isc/include/isc/string.h @@ -32,13 +32,6 @@ ISC_LANG_BEGINDECLS -char * -isc_string_separate(char **stringp, const char *delim); - -#ifdef ISC_PLATFORM_NEEDSTRSEP -#define strsep isc_string_separate -#endif - #ifdef ISC_PLATFORM_NEEDMEMMOVE #define memmove(a,b,c) bcopy(b,a,c) #endif diff --git a/lib/isc/string.c b/lib/isc/string.c index 01ceeb1775..b1b985b6a2 100644 --- a/lib/isc/string.c +++ b/lib/isc/string.c @@ -50,27 +50,6 @@ #include #include -char * -isc_string_separate(char **stringp, const char *delim) { - char *string = *stringp; - char *s; - const char *d; - char sc, dc; - - if (string == NULL) - return (NULL); - - for (s = string; (sc = *s) != '\0'; s++) - for (d = delim; (dc = *d) != '\0'; d++) - if (sc == dc) { - *s++ = '\0'; - *stringp = s; - return (string); - } - *stringp = NULL; - return (string); -} - size_t isc_string_strlcpy(char *dst, const char *src, size_t size) { diff --git a/lib/isc/win32/libisc.def.in b/lib/isc/win32/libisc.def.in index a5f1960e95..fe4a6c2057 100644 --- a/lib/isc/win32/libisc.def.in +++ b/lib/isc/win32/libisc.def.in @@ -645,7 +645,6 @@ isc_stdio_sync isc_stdio_tell isc_stdio_write isc_stdtime_get -isc_string_separate isc_string_strcasestr isc_string_strlcat isc_string_strlcpy From 11f26b42e3b72d11fd257c56b417406426bd7e72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 21 Mar 2018 21:16:51 +0000 Subject: [PATCH 06/10] Replace all usage of non-reentrant strtok() with strtok_r() --- bin/named/main.c | 7 ++++--- bin/tests/system/dlzexternal/driver.c | 18 +++++------------- contrib/dlz/example/dlz_example.c | 18 +++++------------- .../dlz/modules/mysqldyn/dlz_mysqldyn_mod.c | 18 +++++------------- contrib/queryperf/queryperf.c | 14 +++++++------- contrib/sdb/bdb/bdb.c | 10 ++++++---- lib/isc/win32/fsaccess.c | 5 +++-- lib/isc/win32/include/isc/platform.h.in | 3 ++- 8 files changed, 37 insertions(+), 56 deletions(-) diff --git a/bin/named/main.c b/bin/named/main.c index c19206215e..5307b08378 100644 --- a/bin/named/main.c +++ b/bin/named/main.c @@ -607,14 +607,15 @@ parse_command_line(int argc, char *argv[]) { else if (!strncmp(isc_commandline_argument, "mkeytimers=", 11)) { - p = strtok(isc_commandline_argument + 11, "/"); + char *last; + p = strtok_r(isc_commandline_argument + 11, "/", &last); if (p == NULL) named_main_earlyfatal("bad mkeytimer"); dns_zone_mkey_hour = atoi(p); if (dns_zone_mkey_hour == 0) named_main_earlyfatal("bad mkeytimer"); - p = strtok(NULL, "/"); + p = strtok_r(NULL, "/", &last); if (p == NULL) { dns_zone_mkey_day = (24 * dns_zone_mkey_hour); @@ -626,7 +627,7 @@ parse_command_line(int argc, char *argv[]) { if (dns_zone_mkey_day < dns_zone_mkey_hour) named_main_earlyfatal("bad mkeytimer"); - p = strtok(NULL, "/"); + p = strtok_r(NULL, "/", &last); if (p == NULL) { dns_zone_mkey_month = (30 * dns_zone_mkey_day); diff --git a/bin/tests/system/dlzexternal/driver.c b/bin/tests/system/dlzexternal/driver.c index 71967d86df..a55189126b 100644 --- a/bin/tests/system/dlzexternal/driver.c +++ b/bin/tests/system/dlzexternal/driver.c @@ -31,14 +31,6 @@ #include "driver.h" -#ifdef WIN32 -#define STRTOK_R(a, b, c) strtok_s(a, b, c) -#elif defined(_REENTRANT) -#define STRTOK_R(a, b, c) strtok_r(a, b, c) -#else -#define STRTOK_R(a, b, c) strtok(a, b) -#endif - #define CHECK(x) \ do { \ result = (x); \ @@ -726,23 +718,23 @@ modrdataset(struct dlz_example_data *state, const char *name, * for the type used by dig */ - full_name = STRTOK_R(buf, "\t", &saveptr); + full_name = strtok_r(buf, "\t", &saveptr); if (full_name == NULL) goto error; - ttlstr = STRTOK_R(NULL, "\t", &saveptr); + ttlstr = strtok_r(NULL, "\t", &saveptr); if (ttlstr == NULL) goto error; - dclass = STRTOK_R(NULL, "\t", &saveptr); + dclass = strtok_r(NULL, "\t", &saveptr); if (dclass == NULL) goto error; - type = STRTOK_R(NULL, "\t", &saveptr); + type = strtok_r(NULL, "\t", &saveptr); if (type == NULL) goto error; - data = STRTOK_R(NULL, "\t", &saveptr); + data = strtok_r(NULL, "\t", &saveptr); if (data == NULL) goto error; diff --git a/contrib/dlz/example/dlz_example.c b/contrib/dlz/example/dlz_example.c index 3a63dc4b9f..848189903d 100644 --- a/contrib/dlz/example/dlz_example.c +++ b/contrib/dlz/example/dlz_example.c @@ -27,14 +27,6 @@ #include "../modules/include/dlz_minimal.h" -#ifdef WIN32 -#define STRTOK_R(a, b, c) strtok_s(a, b, c) -#elif defined(_REENTRANT) -#define STRTOK_R(a, b, c) strtok_r(a, b, c) -#else -#define STRTOK_R(a, b, c) strtok(a, b) -#endif - #define CHECK(x) \ do { \ result = (x); \ @@ -675,23 +667,23 @@ modrdataset(struct dlz_example_data *state, const char *name, * for the type used by dig */ - full_name = STRTOK_R(buf, "\t", &saveptr); + full_name = strtok_r(buf, "\t", &saveptr); if (full_name == NULL) goto error; - ttlstr = STRTOK_R(NULL, "\t", &saveptr); + ttlstr = strtok_r(NULL, "\t", &saveptr); if (ttlstr == NULL) goto error; - dclass = STRTOK_R(NULL, "\t", &saveptr); + dclass = strtok_r(NULL, "\t", &saveptr); if (dclass == NULL) goto error; - type = STRTOK_R(NULL, "\t", &saveptr); + type = strtok_r(NULL, "\t", &saveptr); if (type == NULL) goto error; - data = STRTOK_R(NULL, "\t", &saveptr); + data = strtok_r(NULL, "\t", &saveptr); if (data == NULL) goto error; diff --git a/contrib/dlz/modules/mysqldyn/dlz_mysqldyn_mod.c b/contrib/dlz/modules/mysqldyn/dlz_mysqldyn_mod.c index 1e8cdad2bb..7bc5398750 100644 --- a/contrib/dlz/modules/mysqldyn/dlz_mysqldyn_mod.c +++ b/contrib/dlz/modules/mysqldyn/dlz_mysqldyn_mod.c @@ -179,14 +179,6 @@ "DELETE FROM ZoneData WHERE zone_id = %s AND " \ "LOWER(name) = LOWER('%s') AND UPPER(type) = UPPER('%s')" -#ifdef WIN32 -#define STRTOK_R(a, b, c) strtok_s(a, b, c) -#elif defined(_REENTRANT) -#define STRTOK_R(a, b, c) strtok_r(a, b, c) -#else -#define STRTOK_R(a, b, c) strtok(a, b) -#endif - /* * Number of concurrent database connections we support * - equivalent to maxmium number of concurrent transactions @@ -837,23 +829,23 @@ makerecord(mysql_data_t *state, const char *name, const char *rdatastr) { * The DATA field is space separated, and is in the data format * for the type used by dig */ - real_name = STRTOK_R(buf, "\t", &saveptr); + real_name = strtok_r(buf, "\t", &saveptr); if (real_name == NULL) goto error; - ttlstr = STRTOK_R(NULL, "\t", &saveptr); + ttlstr = strtok_r(NULL, "\t", &saveptr); if (ttlstr == NULL || sscanf(ttlstr, "%d", &ttlvalue) != 1) goto error; - dclass = STRTOK_R(NULL, "\t", &saveptr); + dclass = strtok_r(NULL, "\t", &saveptr); if (dclass == NULL) goto error; - type = STRTOK_R(NULL, "\t", &saveptr); + type = strtok_r(NULL, "\t", &saveptr); if (type == NULL) goto error; - data = STRTOK_R(NULL, "\t", &saveptr); + data = strtok_r(NULL, "\t", &saveptr); if (data == NULL) goto error; diff --git a/contrib/queryperf/queryperf.c b/contrib/queryperf/queryperf.c index 0738cc2903..28a62ea019 100644 --- a/contrib/queryperf/queryperf.c +++ b/contrib/queryperf/queryperf.c @@ -1184,7 +1184,7 @@ identify_directive(char *dir) { */ void update_config(char *config_change_desc) { - char *directive, *config_value, *trailing_garbage; + char *directive, *config_value, *trailing_garbage, *last; char conf_copy[MAX_INPUT_LEN + 1]; unsigned int uint_val; int directive_number; @@ -1213,9 +1213,9 @@ update_config(char *config_change_desc) { return; } - directive = strtok(config_change_desc, WHITESPACE); - config_value = strtok(NULL, WHITESPACE); - trailing_garbage = strtok(NULL, WHITESPACE); + directive = strtok_r(config_change_desc, WHITESPACE, &last); + config_value = strtok_r(NULL, WHITESPACE, &last); + trailing_garbage = strtok_r(NULL, WHITESPACE, &last); if ((directive_number = identify_directive(directive)) == -1) { fprintf(stderr, "Invalid config: Bad directive: %s\n", @@ -1349,7 +1349,7 @@ parse_query(char *input, char *qname, unsigned int qnlen, int *qtype) { unsigned int num_types, index; int found = FALSE; char incopy[MAX_INPUT_LEN + 1]; - char *domain_str, *type_str; + char *domain_str, *type_str, *last; num_types = sizeof(qtype_strings) / sizeof(qtype_strings[0]); if (num_types > (sizeof(qtype_codes) / sizeof(int))) @@ -1357,8 +1357,8 @@ parse_query(char *input, char *qname, unsigned int qnlen, int *qtype) { strcpy(incopy, input); - domain_str = strtok(incopy, WHITESPACE); - type_str = strtok(NULL, WHITESPACE); + domain_str = strtok_r(incopy, WHITESPACE, &last); + type_str = strtok_r(NULL, WHITESPACE, &last); if ((domain_str == NULL) || (type_str == NULL)) { fprintf(stderr, "Invalid query input format: %s\n", input); diff --git a/contrib/sdb/bdb/bdb.c b/contrib/sdb/bdb/bdb.c index 23594bbe3c..351bdc8a54 100644 --- a/contrib/sdb/bdb/bdb.c +++ b/contrib/sdb/bdb/bdb.c @@ -137,12 +137,13 @@ bdb_lookup(const char *zone, const char *name, void *dbdata, ret = c->c_get(c, &key, &data, DB_SET); while (ret == 0) { + char *last; ((char *)key.data)[key.size] = 0; ((char *)data.data)[data.size] = 0; - ttltext.base = strtok((char *)data.data, " "); + ttltext.base = strtok_r((char *)data.data, " ", &last); ttltext.length = strlen(ttltext.base); dns_ttl_fromtext((isc_textregion_t *)&ttltext, &ttl); - type = strtok(NULL, " "); + type = strtok_r(NULL, " ", &last); rdata = type + strlen(type) + 1; if (dns_sdb_putrr(l, type, ttl, rdata) != ISC_R_SUCCESS) { @@ -185,12 +186,13 @@ bdb_allnodes(const char *zone, void *dbdata, dns_sdballnodes_t *n) memset(&data, 0, sizeof(DBT)); while (c->c_get(c, &key, &data, DB_NEXT) == 0) { + char *last; ((char *)key.data)[key.size] = 0; ((char *)data.data)[data.size] = 0; - ttltext.base = strtok((char *)data.data, " "); + ttltext.base = strtok_r((char *)data.data, " ", &last); ttltext.length = strlen(ttltext.base); dns_ttl_fromtext((isc_textregion_t *)&ttltext, &ttl); - type = strtok(NULL, " "); + type = strtok_r(NULL, " ", &last); rdata = type + strlen(type) + 1; if (dns_sdb_putnamedrr(n, key.data, type, ttl, rdata) != diff --git a/lib/isc/win32/fsaccess.c b/lib/isc/win32/fsaccess.c index 5944f54355..9b3a6559eb 100644 --- a/lib/isc/win32/fsaccess.c +++ b/lib/isc/win32/fsaccess.c @@ -76,10 +76,11 @@ is_ntfs(const char * file) { /* Copy 'c:\' or 'c:/' and NUL terminate. */ strlcpy(drive, filename, ISC_MIN(3 + 1, sizeof(drive))); } else if ((filename[0] == '\\') && (filename[1] == '\\')) { + char *last; /* Find the machine and share name and rebuild the UNC */ strlcpy(tmpbuf, filename, sizeof(tmpbuf)); - machinename = strtok(tmpbuf, "\\"); - sharename = strtok(NULL, "\\"); + machinename = strtok_r(tmpbuf, "\\", &last); + sharename = strtok_r(NULL, "\\", &last); strlcpy(drive, "\\\\", sizeof(drive)); strlcat(drive, machinename, sizeof(drive)); strlcat(drive, "\\", sizeof(drive)); diff --git a/lib/isc/win32/include/isc/platform.h.in b/lib/isc/win32/include/isc/platform.h.in index e3f534f7b8..2e79ae450b 100644 --- a/lib/isc/win32/include/isc/platform.h.in +++ b/lib/isc/win32/include/isc/platform.h.in @@ -25,11 +25,12 @@ */ #if defined(_WIN32) || defined(_WIN64) +/* We are on Windows */ +# define strtok_r strtok_s #ifndef strtoull #define strtoull _strtoui64 #endif - #endif /*** From 7278c455bc24bb39af469f5af3444c2ef630da2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Thu, 5 Apr 2018 13:44:25 +0200 Subject: [PATCH 07/10] Remove isc_string_strcasestr implementation and clean up the header and headers --- lib/isc/include/isc/string.h | 33 +++------------------------ lib/isc/string.c | 43 +++++++++--------------------------- lib/isc/win32/libisc.def.in | 1 - 3 files changed, 14 insertions(+), 63 deletions(-) diff --git a/lib/isc/include/isc/string.h b/lib/isc/include/isc/string.h index 67ed24e615..9750163aa9 100644 --- a/lib/isc/include/isc/string.h +++ b/lib/isc/include/isc/string.h @@ -9,42 +9,24 @@ * information regarding copyright ownership. */ -/* $Id: string.h,v 1.23 2007/09/13 04:48:16 each Exp $ */ - -#ifndef ISC_STRING_H -#define ISC_STRING_H 1 +#pragma once /*! \file isc/string.h */ -#include -#include -#include -#include -#include - #include -#ifdef ISC_PLATFORM_HAVESTRINGSH -#include -#endif - -#define ISC_STRING_MAGIC 0x5e +#include "isc/platform.h" +#include "isc/lang.h" ISC_LANG_BEGINDECLS -#ifdef ISC_PLATFORM_NEEDMEMMOVE -#define memmove(a,b,c) bcopy(b,a,c) -#endif - size_t isc_string_strlcpy(char *dst, const char *src, size_t size); - #ifdef ISC_PLATFORM_NEEDSTRLCPY #define strlcpy isc_string_strlcpy #endif - size_t isc_string_strlcat(char *dst, const char *src, size_t size); @@ -52,13 +34,4 @@ isc_string_strlcat(char *dst, const char *src, size_t size); #define strlcat isc_string_strlcat #endif -char * -isc_string_strcasestr(const char *big, const char *little); - -#ifdef ISC_PLATFORM_NEEDSTRCASESTR -#define strcasestr isc_string_strcasestr -#endif - ISC_LANG_ENDDECLS - -#endif /* ISC_STRING_H */ diff --git a/lib/isc/string.c b/lib/isc/string.c index b1b985b6a2..ff37f61944 100644 --- a/lib/isc/string.c +++ b/lib/isc/string.c @@ -40,15 +40,11 @@ /*! \file */ -#include +#include // IWYU pragma: keep -#include +#include -#include -#include -#include -#include -#include +#include "isc/string.h" // IWYU pragma: keep size_t isc_string_strlcpy(char *dst, const char *src, size_t size) @@ -60,15 +56,17 @@ isc_string_strlcpy(char *dst, const char *src, size_t size) /* Copy as many bytes as will fit */ if (n != 0U && --n != 0U) { do { - if ((*d++ = *s++) == 0) + if ((*d++ = *s++) == 0) { break; + } } while (--n != 0U); } /* Not enough room in dst, add NUL and traverse rest of src */ if (n == 0U) { - if (size != 0U) + if (size != 0U) { *d = '\0'; /* NUL-terminate dst */ + } while (*s++) ; } @@ -85,13 +83,15 @@ isc_string_strlcat(char *dst, const char *src, size_t size) size_t dlen; /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0U && *d != '\0') + while (n-- != 0U && *d != '\0') { d++; + } dlen = d - dst; n = size - dlen; - if (n == 0U) + if (n == 0U) { return(dlen + strlen(s)); + } while (*s != '\0') { if (n != 1U) { *d++ = *s; @@ -103,24 +103,3 @@ isc_string_strlcat(char *dst, const char *src, size_t size) return(dlen + (s - src)); /* count does not include NUL */ } - -char * -isc_string_strcasestr(const char *str, const char *search) { - char c, sc, *s; - size_t len; - - if ((c = *search++) != 0) { - c = tolower((unsigned char) c); - len = strlen(search); - do { - do { - if ((sc = *str++) == 0) - return (NULL); - } while ((char) tolower((unsigned char) sc) != c); - } while (strncasecmp(str, search, len) != 0); - str--; - } - DE_CONST(str, s); - return (s); - -} diff --git a/lib/isc/win32/libisc.def.in b/lib/isc/win32/libisc.def.in index fe4a6c2057..72687aee2f 100644 --- a/lib/isc/win32/libisc.def.in +++ b/lib/isc/win32/libisc.def.in @@ -645,7 +645,6 @@ isc_stdio_sync isc_stdio_tell isc_stdio_write isc_stdtime_get -isc_string_strcasestr isc_string_strlcat isc_string_strlcpy isc_symtab_count From 96a07ba867d90c54f28ef364f0eda327007a70ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 9 Apr 2018 09:29:02 +0200 Subject: [PATCH 08/10] Remove usage of strcasestr, so we don't have to have replacement function on Windows --- bin/named/statschannel.c | 51 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/bin/named/statschannel.c b/bin/named/statschannel.c index 760aaa7952..42d87a0352 100644 --- a/bin/named/statschannel.c +++ b/bin/named/statschannel.c @@ -3106,6 +3106,7 @@ render_xsl(const char *url, isc_httpdurl_t *urlinfo, isc_httpdfree_t **freecb, void **freecb_args) { isc_result_t result; + char *_headers = NULL; UNUSED(url); UNUSED(querystring); @@ -3117,30 +3118,39 @@ render_xsl(const char *url, isc_httpdurl_t *urlinfo, if (urlinfo->isstatic) { isc_time_t when; - char *p = strcasestr(headers, "If-Modified-Since: "); + char *line, *saveptr; + const char *if_modified_since = "If-Modified-Since: "; + _headers = strdup(headers); - if (p != NULL) { - time_t t1, t2; - p += strlen("If-Modified-Since: "); - result = isc_time_parsehttptimestamp(p, &when); - if (result != ISC_R_SUCCESS) - goto send; + for (line = strtok_r(_headers, "\n", &saveptr); + line; + line = strtok_r(NULL, "\n", &saveptr)) { + if (strncasecmp(line, if_modified_since, strlen(if_modified_since)) == 0) { + time_t t1, t2; + line += strlen(if_modified_since); + result = isc_time_parsehttptimestamp(line, &when); + if (result != ISC_R_SUCCESS) { + goto send; + } - result = isc_time_secondsastimet(&when, &t1); - if (result != ISC_R_SUCCESS) - goto send; + result = isc_time_secondsastimet(&when, &t1); + if (result != ISC_R_SUCCESS) { + goto send; + } - result = isc_time_secondsastimet(&urlinfo->loadtime, - &t2); - if (result != ISC_R_SUCCESS) - goto send; + result = isc_time_secondsastimet(&urlinfo->loadtime, &t2); + if (result != ISC_R_SUCCESS) { + goto send; + } - if (t1 < t2) - goto send; + if (t1 < t2) { + goto send; + } - *retcode = 304; - *retmsg = "Not modified"; - return (ISC_R_SUCCESS); + *retcode = 304; + *retmsg = "Not modified"; + goto end; + } } } @@ -3149,7 +3159,8 @@ render_xsl(const char *url, isc_httpdurl_t *urlinfo, *retmsg = "OK"; isc_buffer_reinit(b, xslmsg, strlen(xslmsg)); isc_buffer_add(b, strlen(xslmsg)); - +end: + free(_headers); return (ISC_R_SUCCESS); } From 5311a3b7b5f7b810feaa386cdc036da83e209c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 11 Apr 2018 15:16:34 +0200 Subject: [PATCH 09/10] We use too old Visual Compiler to use scoped variables --- bin/dig/dig.c | 5 ++--- bin/named/main.c | 2 +- bin/tools/mdig.c | 2 +- contrib/dlz/drivers/sdlz_helper.c | 3 ++- contrib/sdb/bdb/bdb.c | 2 +- lib/irs/getaddrinfo.c | 3 +-- lib/isc/win32/fsaccess.c | 2 +- 7 files changed, 9 insertions(+), 10 deletions(-) diff --git a/bin/dig/dig.c b/bin/dig/dig.c index dcd11f14fc..215faa155f 100644 --- a/bin/dig/dig.c +++ b/bin/dig/dig.c @@ -733,7 +733,7 @@ plus_option(char *option, isc_boolean_t is_batchfile, dig_lookup_t *lookup) { isc_result_t result; - char *cmd, *value, *last, *code; + char *cmd, *value, *last, *code, *extra; isc_uint32_t num; isc_boolean_t state = ISC_TRUE; size_t n; @@ -1003,7 +1003,6 @@ plus_option(char *option, isc_boolean_t is_batchfile, "specified"); goto exit_or_usage; } - char *extra; code = strtok_r(value, ":", &last); extra = strtok_r(NULL, "\0", &last); save_opt(lookup, code, extra); @@ -2137,6 +2136,7 @@ query_finished(void) { char batchline[MXNAME]; int bargc; char *bargv[16]; + char *last; if (batchname == NULL) { isc_app_shutdown(); @@ -2153,7 +2153,6 @@ query_finished(void) { } if (fgets(batchline, sizeof(batchline), batchfp) != 0) { - char *last; debug("batch line %s", batchline); for (bargc = 1, bargv[bargc] = strtok_r(batchline, " \t\r\n", &last); bargc < 14 && bargv[bargc]; diff --git a/bin/named/main.c b/bin/named/main.c index 5307b08378..526779befa 100644 --- a/bin/named/main.c +++ b/bin/named/main.c @@ -470,6 +470,7 @@ parse_command_line(int argc, char *argv[]) { int ch; int port; const char *p; + char *last; save_command_line(argc, argv); @@ -607,7 +608,6 @@ parse_command_line(int argc, char *argv[]) { else if (!strncmp(isc_commandline_argument, "mkeytimers=", 11)) { - char *last; p = strtok_r(isc_commandline_argument + 11, "/", &last); if (p == NULL) named_main_earlyfatal("bad mkeytimer"); diff --git a/bin/tools/mdig.c b/bin/tools/mdig.c index e9e0c52ede..6edb96e8e4 100644 --- a/bin/tools/mdig.c +++ b/bin/tools/mdig.c @@ -1741,6 +1741,7 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) int rc; char **rv; isc_boolean_t global = ISC_TRUE; + char *last; /* * The semantics for parsing the args is a bit complex; if @@ -1852,7 +1853,6 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) fatal("couldn't open batch file '%s'", batchname); } while (fgets(batchline, sizeof(batchline), batchfp) != 0) { - char *last; if (batchline[0] == '\r' || batchline[0] == '\n' || batchline[0] == '#' || batchline[0] == ';') continue; diff --git a/contrib/dlz/drivers/sdlz_helper.c b/contrib/dlz/drivers/sdlz_helper.c index 46bb61dcb7..ca79f4ccd6 100644 --- a/contrib/dlz/drivers/sdlz_helper.c +++ b/contrib/dlz/drivers/sdlz_helper.c @@ -108,6 +108,7 @@ build_querylist(isc_mem_t *mctx, const char *query_str, char **zone, char *right_str = NULL; query_list_t *tql; query_segment_t *tseg = NULL; + char *last; REQUIRE(querylist != NULL && *querylist == NULL); REQUIRE(mctx != NULL); @@ -158,7 +159,7 @@ build_querylist(isc_mem_t *mctx, const char *query_str, char **zone, * split string at the first "$". set query segment to * left portion */ - char *last = NULL; + last = NULL; tseg->sql = isc_mem_strdup(mctx, strtok_r(right_str, "$", &last)); if (tseg->sql == NULL) { diff --git a/contrib/sdb/bdb/bdb.c b/contrib/sdb/bdb/bdb.c index 351bdc8a54..8d514abb2d 100644 --- a/contrib/sdb/bdb/bdb.c +++ b/contrib/sdb/bdb/bdb.c @@ -113,6 +113,7 @@ bdb_lookup(const char *zone, const char *name, void *dbdata, isc_consttextregion_t ttltext; DBC *c; DBT key, data; + char *last; UNUSED(zone); #ifdef DNS_CLIENTINFO_VERSION @@ -137,7 +138,6 @@ bdb_lookup(const char *zone, const char *name, void *dbdata, ret = c->c_get(c, &key, &data, DB_SET); while (ret == 0) { - char *last; ((char *)key.data)[key.size] = 0; ((char *)data.data)[data.size] = 0; ttltext.base = strtok_r((char *)data.data, " ", &last); diff --git a/lib/irs/getaddrinfo.c b/lib/irs/getaddrinfo.c index 2aed01c315..f3172543f1 100644 --- a/lib/irs/getaddrinfo.c +++ b/lib/irs/getaddrinfo.c @@ -1055,7 +1055,7 @@ static void set_order(int family, int (**net_order)(const char *, int, struct addrinfo **, int, int)) { - char *order, *tok; + char *order, *tok, *last; int found; if (family) { @@ -1070,7 +1070,6 @@ set_order(int family, int (**net_order)(const char *, int, struct addrinfo **, } else { order = getenv("NET_ORDER"); found = 0; - char *last; for (tok = strtok_r(order, ":", &last); tok; tok = strtok_r(NULL, ":", &last)) diff --git a/lib/isc/win32/fsaccess.c b/lib/isc/win32/fsaccess.c index 9b3a6559eb..916a4b13af 100644 --- a/lib/isc/win32/fsaccess.c +++ b/lib/isc/win32/fsaccess.c @@ -59,6 +59,7 @@ is_ntfs(const char * file) { char *machinename; char *sharename; char filename[1024]; + char *last; REQUIRE(filename != NULL); @@ -76,7 +77,6 @@ is_ntfs(const char * file) { /* Copy 'c:\' or 'c:/' and NUL terminate. */ strlcpy(drive, filename, ISC_MIN(3 + 1, sizeof(drive))); } else if ((filename[0] == '\\') && (filename[1] == '\\')) { - char *last; /* Find the machine and share name and rebuild the UNC */ strlcpy(tmpbuf, filename, sizeof(tmpbuf)); machinename = strtok_r(tmpbuf, "\\", &last); From 23f97f9a6611dcae4f051fc0b15d6d5596c68345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Thu, 5 Apr 2018 13:45:30 +0200 Subject: [PATCH 10/10] Add CHANGES entry: 4924. [cleanup] Clean up the isc_string_* namespace and leave only strlcpy and strlcat. [GL #178] --- CHANGES | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES b/CHANGES index 64a2bd3e65..995e418736 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +4924. [cleanup] Clean up the isc_string_* namespace and leave + only strlcpy and strlcat. [GL #178] + 4923. [cleanup] Refactor socket and socket event options into enum types. [GL !135]