]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
more str{n}{cat,cpy} corrections rt45981_stage2
authorMark Andrews <marka@isc.org>
Thu, 14 Sep 2017 08:11:56 +0000 (18:11 +1000)
committerMark Andrews <marka@isc.org>
Thu, 14 Sep 2017 08:11:56 +0000 (18:11 +1000)
26 files changed:
bin/delv/delv.c
bin/dig/dig.c
bin/dig/host.c
bin/dig/nslookup.c
bin/named/win32/ntservice.c
bin/nsupdate/nsupdate.c
bin/tests/sock_test.c
bin/tools/mdig.c
lib/dns/rcode.c
lib/dns/rdata.c
lib/dns/ttl.c
lib/dns/zone.c
lib/irs/getaddrinfo.c
lib/isc/mem.c
lib/isc/pk11.c
lib/isc/task.c
lib/isc/unix/file.c
lib/isc/unix/interfaceiter.c
lib/isc/unix/socket.c
lib/isc/unix/time.c
lib/isc/win32/file.c
lib/isc/win32/fsaccess.c
lib/isc/win32/ntpaths.c
lib/isc/win32/socket.c
lib/isc/win32/time.c
lib/ns/interfacemgr.c

index 9b96154ec3d1a08e59d2380984ed107beed1d0d8..2560331143a9c045eaa528a2b2a0e79d29d93d88 100644 (file)
@@ -978,8 +978,7 @@ plus_option(char *option) {
        char *cmd, *value, *ptr;
        isc_boolean_t state = ISC_TRUE;
 
-       strncpy(option_store, option, sizeof(option_store));
-       option_store[sizeof(option_store)-1]=0;
+       strlcpy(option_store, option, sizeof(option_store));
        ptr = option_store;
        cmd = next_token(&ptr,"=");
        if (cmd == NULL) {
index 6735c1b84b4b11ebf0771555524341cc0acdb1c1..562ef79a73940e974317fbd80452e492d56d396e 100644 (file)
@@ -669,33 +669,27 @@ cleanup:
 static void
 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
        int i;
-       size_t remaining;
        static isc_boolean_t first = ISC_TRUE;
        char append[MXNAME];
 
        if (printcmd) {
-               lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
                snprintf(lookup->cmdline, sizeof(lookup->cmdline),
                         "%s; <<>> DiG " VERSION " <<>>",
                         first?"\n":"");
                i = 1;
                while (i < argc) {
                        snprintf(append, sizeof(append), " %s", argv[i++]);
-                       remaining = sizeof(lookup->cmdline) -
-                                   strlen(lookup->cmdline) - 1;
-                       strncat(lookup->cmdline, append, remaining);
+                       strlcat(lookup->cmdline, append,
+                               sizeof(lookup->cmdline));
                }
-               remaining = sizeof(lookup->cmdline) -
-                           strlen(lookup->cmdline) - 1;
-               strncat(lookup->cmdline, "\n", remaining);
+               strlcat(lookup->cmdline, "\n", sizeof(lookup->cmdline));
                if (first && addresscount != 0) {
                        snprintf(append, sizeof(append),
                                 "; (%d server%s found)\n",
                                 addresscount,
                                 addresscount > 1 ? "s" : "");
-                       remaining = sizeof(lookup->cmdline) -
-                                   strlen(lookup->cmdline) - 1;
-                       strncat(lookup->cmdline, append, remaining);
+                       strlcat(lookup->cmdline, append,
+                               sizeof(lookup->cmdline));
                }
                if (first) {
                        snprintf(append, sizeof(append),
@@ -703,9 +697,8 @@ printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
                                 short_form ? " +short" : "",
                                 printcmd ? " +cmd" : "");
                        first = ISC_FALSE;
-                       remaining = sizeof(lookup->cmdline) -
-                                   strlen(lookup->cmdline) - 1;
-                       strncat(lookup->cmdline, append, remaining);
+                       strlcat(lookup->cmdline, append,
+                               sizeof(lookup->cmdline));
                }
        }
 }
@@ -728,8 +721,7 @@ plus_option(const char *option, isc_boolean_t is_batchfile,
        isc_boolean_t state = ISC_TRUE;
        size_t n;
 
-       strncpy(option_store, option, sizeof(option_store));
-       option_store[sizeof(option_store)-1]=0;
+       strlcpy(option_store, option, sizeof(option_store));
        ptr = option_store;
        cmd = next_token(&ptr, "=");
        if (cmd == NULL) {
@@ -905,8 +897,7 @@ plus_option(const char *option, isc_boolean_t is_batchfile,
                                goto need_value;
                        if (!state)
                                goto invalid_option;
-                       strncpy(domainopt, value, sizeof(domainopt));
-                       domainopt[sizeof(domainopt)-1] = '\0';
+                       strlcpy(domainopt, value, sizeof(domainopt));
                        break;
                case 's': /* dscp */
                        FULLCHECK("dscp");
@@ -1598,8 +1589,7 @@ dash_option(char *option, char *next, dig_lookup_t **lookup,
                batchname = value;
                return (value_from_next);
        case 'k':
-               strncpy(keyfile, value, sizeof(keyfile));
-               keyfile[sizeof(keyfile)-1]=0;
+               strlcpy(keyfile, value, sizeof(keyfile));
                return (value_from_next);
        case 'p':
                result = parse_uint(&num, value, MAXPORT, "port number");
@@ -1613,9 +1603,8 @@ dash_option(char *option, char *next, dig_lookup_t **lookup,
                                (*lookup) = clone_lookup(default_lookup,
                                                         ISC_TRUE);
                        *need_clone = ISC_TRUE;
-                       strncpy((*lookup)->textname, value,
+                       strlcpy((*lookup)->textname, value,
                                sizeof((*lookup)->textname));
-                       (*lookup)->textname[sizeof((*lookup)->textname)-1]=0;
                        (*lookup)->trace_root = ISC_TF((*lookup)->trace  ||
                                                     (*lookup)->ns_search_only);
                        (*lookup)->new_search = ISC_TRUE;
@@ -1699,10 +1688,8 @@ dash_option(char *option, char *next, dig_lookup_t **lookup,
 #endif
                        digestbits = 0;
                }
-               strncpy(keynametext, ptr, sizeof(keynametext));
-               keynametext[sizeof(keynametext)-1]=0;
-               strncpy(keysecret, ptr2, sizeof(keysecret));
-               keysecret[sizeof(keysecret)-1]=0;
+               strlcpy(keynametext, ptr, sizeof(keynametext));
+               strlcpy(keysecret, ptr2, sizeof(keysecret));
                return (value_from_next);
        case 'x':
                if (*need_clone)
@@ -1710,9 +1697,8 @@ dash_option(char *option, char *next, dig_lookup_t **lookup,
                *need_clone = ISC_TRUE;
                if (get_reverse(textname, sizeof(textname), value,
                                ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
-                       strncpy((*lookup)->textname, textname,
+                       strlcpy((*lookup)->textname, textname,
                                sizeof((*lookup)->textname));
-                       (*lookup)->textname[sizeof((*lookup)->textname)-1] = 0;
                        debug("looking up %s", (*lookup)->textname);
                        (*lookup)->trace_root = ISC_TF((*lookup)->trace  ||
                                                (*lookup)->ns_search_only);
@@ -2006,9 +1992,8 @@ parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
                                        lookup = clone_lookup(default_lookup,
                                                                      ISC_TRUE);
                                need_clone = ISC_TRUE;
-                               strncpy(lookup->textname, rv[0],
+                               strlcpy(lookup->textname, rv[0],
                                        sizeof(lookup->textname));
-                               lookup->textname[sizeof(lookup->textname)-1]=0;
                                lookup->trace_root = ISC_TF(lookup->trace  ||
                                                     lookup->ns_search_only);
                                lookup->new_search = ISC_TRUE;
index 38bdba3faaa17576abaaa9c96e9c97f6a4cc288c..13800472e14a87fa8db9c6eab3202eb84a653c65 100644 (file)
@@ -463,9 +463,8 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
                dns_name_format(name, namestr, sizeof(namestr));
                lookup = clone_lookup(query->lookup, ISC_FALSE);
                if (lookup != NULL) {
-                       strncpy(lookup->textname, namestr,
+                       strlcpy(lookup->textname, namestr,
                                sizeof(lookup->textname));
-                       lookup->textname[sizeof(lookup->textname)-1] = 0;
                        lookup->rdtype = dns_rdatatype_aaaa;
                        lookup->rdtypeset = ISC_TRUE;
                        lookup->origin = NULL;
@@ -474,9 +473,8 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
                }
                lookup = clone_lookup(query->lookup, ISC_FALSE);
                if (lookup != NULL) {
-                       strncpy(lookup->textname, namestr,
+                       strlcpy(lookup->textname, namestr,
                                sizeof(lookup->textname));
-                       lookup->textname[sizeof(lookup->textname)-1] = 0;
                        lookup->rdtype = dns_rdatatype_mx;
                        lookup->rdtypeset = ISC_TRUE;
                        lookup->origin = NULL;
@@ -861,14 +859,12 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
        lookup->pending = ISC_FALSE;
        if (get_reverse(store, sizeof(store), hostname,
                        lookup->ip6_int, ISC_TRUE) == ISC_R_SUCCESS) {
-               strncpy(lookup->textname, store, sizeof(lookup->textname));
-               lookup->textname[sizeof(lookup->textname)-1] = 0;
+               strlcpy(lookup->textname, store, sizeof(lookup->textname));
                lookup->rdtype = dns_rdatatype_ptr;
                lookup->rdtypeset = ISC_TRUE;
                default_lookups = ISC_FALSE;
        } else {
-               strncpy(lookup->textname, hostname, sizeof(lookup->textname));
-               lookup->textname[sizeof(lookup->textname)-1]=0;
+               strlcpy(lookup->textname, hostname, sizeof(lookup->textname));
                usesearch = ISC_TRUE;
        }
        lookup->new_search = ISC_TRUE;
index 314d86b3c67e8b4e12a756563c94fcbb5b1d7f70..131c9d6b4fc1c709673401119cc088a4cfab49fc 100644 (file)
@@ -479,9 +479,8 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
                dns_name_format(name, namestr, sizeof(namestr));
                lookup = clone_lookup(query->lookup, ISC_FALSE);
                if (lookup != NULL) {
-                       strncpy(lookup->textname, namestr,
+                       strlcpy(lookup->textname, namestr,
                                sizeof(lookup->textname));
-                       lookup->textname[sizeof(lookup->textname)-1] = 0;
                        lookup->rdtype = dns_rdatatype_aaaa;
                        lookup->rdtypeset = ISC_TRUE;
                        lookup->origin = NULL;
index 71610ec6307df62ebb3c24edc7211fc283c7e9ab..17bb50527a4331807cc5a33b1ccd29194a97f7df 100644 (file)
@@ -49,8 +49,8 @@ ntservice_init(void) {
                }
                UpdateSCM(SERVICE_RUNNING);
        } else {
-               strcpy(ConsoleTitle, "BIND Version ");
-               strcat(ConsoleTitle, VERSION);
+               strlcpy(ConsoleTitle, "BIND Version ", sizeof(ConsoleTitle));
+               strlcat(ConsoleTitle, VERSION, sizeof(ConsoleTitle));
                SetConsoleTitle(ConsoleTitle);
        }
 }
index e5436e753cff8f5ffa27430c787e739ae5e0a787..2fc565c681d65cce1d72cc5a5ff7d2fa35085d81 100644 (file)
@@ -453,8 +453,8 @@ parse_hmac(const dns_name_t **hmac, const char *hmacstr, size_t len,
                return (ISC_FALSE);
        }
 
-       strncpy(buf, hmacstr, len);
-       buf[len] = 0;
+       /* Copy len bytes and NUL terminate. */
+       strlcpy(buf, hmacstr, ISC_MIN(len + 1, sizeof(buf)));
 
 #ifndef PK11_MD5_DISABLE
        if (strcasecmp(buf, "hmac-md5") == 0) {
index 0c68552cf0bae5b833c58a221b2d9ec2a2ca17ac..0ade9e7444f8dd16c1c3b01632ea353637faf037 100644 (file)
@@ -106,7 +106,7 @@ my_recv(isc_task_t *task, isc_event_t *event) {
                region.base = isc_mem_get(mctx, strlen(buf) + 1);
                if (region.base != NULL) {
                        region.length = strlen(buf) + 1;
-                       strcpy((char *)region.base, buf);  /* strcpy is safe */
+                       strlcpy((char *)region.base, buf, region.length);
                } else
                        region.length = 0;
                isc_socket_send(sock, &region, task, my_send, event->ev_arg);
@@ -179,7 +179,7 @@ my_connect(isc_task_t *task, isc_event_t *event) {
        region.base = isc_mem_get(mctx, strlen(buf) + 1);
        if (region.base != NULL) {
                region.length = strlen(buf) + 1;
-               strcpy((char *)region.base, buf);  /* This strcpy is safe. */
+               strlcpy((char *)region.base, buf, region.length);
        } else {
                region.length = 0;
        }
index c1204495a234f93859e100abb560d60b17de0b12..bddf35d2e6de5879f14cdf3737714308c9b84b29 100644 (file)
@@ -1041,8 +1041,7 @@ plus_option(char *option, struct query *query, isc_boolean_t global)
        isc_boolean_t state = ISC_TRUE;
        size_t n;
 
-       strncpy(option_store, option, sizeof(option_store));
-       option_store[sizeof(option_store) - 1] = 0;
+       strlcpy(option_store, option, sizeof(option_store));
        ptr = option_store;
        cmd = next_token(&ptr, "=");
        if (cmd == NULL) {
@@ -1657,8 +1656,7 @@ dash_option(const char *option, char *next, struct query *query,
                return (value_from_next);
        case 'x':
                get_reverse(textname, sizeof(textname), value, query->ip6_int);
-               strncpy(query->textname, textname, sizeof(query->textname));
-               query->textname[sizeof(query->textname) - 1] = 0;
+               strlcpy(query->textname, textname, sizeof(query->textname));
                query->rdtype = dns_rdatatype_ptr;
                query->rdclass = dns_rdataclass_in;
                *setname = ISC_TRUE;
@@ -1845,9 +1843,8 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv)
                         */
                        if (query == &default_query)
                                query = clone_default_query();
-                       strncpy(query->textname, rv[0],
+                       strlcpy(query->textname, rv[0],
                                sizeof(query->textname));
-                       query->textname[sizeof(query->textname) - 1] = 0;
                        ISC_LIST_APPEND(queries, query, link);
 
                        query = clone_default_query();
index 723c0183fbd59a9bcb373bb805e9673ecc30fbf9..8cad290937ec8ed26d2d157bcebac8e7847096c8 100644 (file)
@@ -252,8 +252,8 @@ maybe_numeric(unsigned int *valuep, isc_textregion_t *source,
         * isc_parse_uint32().  isc_parse_uint32() requires
         * null termination, so we must make a copy.
         */
-       strncpy(buffer, source->base, sizeof(buffer));
-       buffer[sizeof(buffer) - 1] = '\0';
+       strlcpy(buffer, source->base,
+               ISC_MIN(source->length + 1, sizeof(buffer)));
 
        INSIST(buffer[source->length] == '\0');
 
@@ -504,8 +504,12 @@ dns_rdataclass_fromtext(dns_rdataclass_t *classp, isc_textregion_t *source) {
                        char *endp;
                        unsigned int val;
 
-                       strncpy(buf, source->base + 5, source->length - 5);
-                       buf[source->length - 5] = '\0';
+                       /*
+                        * source->base is not required to be NUL terminated.
+                        * Copy up to remaining bytes and NUL terminate.
+                        */
+                       strlcpy(buf, source->base + 5,
+                               ISC_MIN(source->length - 5 + 1, sizeof(buf)));
                        val = strtoul(buf, &endp, 10);
                        if (*endp == '\0' && val <= 0xffff) {
                                *classp = (dns_rdataclass_t)val;
index b2e0819174dc744b3dc8f1f25ffb6d44e2dd7ff6..fd55eff695475045a2eb70c796279749de0dcc6b 100644 (file)
@@ -1339,8 +1339,12 @@ dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
                char *endp;
                unsigned int val;
 
-               strncpy(buf, source->base + 4, source->length - 4);
-               buf[source->length - 4] = '\0';
+               /*
+                * source->base is not required to be NUL terminated.
+                * Copy up to remaining bytes and NUL terminate.
+                */
+               strlcpy(buf, source->base + 4,
+                       ISC_MIN(source->length - 4 + 1, sizeof(buf)));
                val = strtoul(buf, &endp, 10);
                if (*endp == '\0' && val <= 0xffff) {
                        *typep = (dns_rdatatype_t)val;
index 35cbc2a3d874b37b58ccc267138764308c8130bd..223d389fcc9378669ef8ae857d89fd2dffb6c6f7 100644 (file)
@@ -159,8 +159,8 @@ bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl) {
         */
        if (source->length > sizeof(buf) - 1)
                return (DNS_R_SYNTAX);
-       strncpy(buf, source->base, source->length);
-       buf[source->length] = '\0';
+       /* Copy source->length bytes and NUL terminate. */
+       strlcpy(buf, source->base, ISC_MIN(source->length + 1, sizeof(buf)));
        s = buf;
 
        do {
index 80c8035b907a99f4a10412d3ebcc9e97c760f902..514c597472a61e83e45b22c3e3e3eda05ef9ab88 100644 (file)
@@ -1372,7 +1372,7 @@ dns_zone_getdbtype(dns_zone_t *zone, char ***argv, isc_mem_t *mctx) {
        unsigned int i;
        isc_result_t result = ISC_R_SUCCESS;
        void *mem;
-       char **tmp, *tmp2;
+       char **tmp, *tmp2, *base;
 
        REQUIRE(DNS_ZONE_VALID(zone));
        REQUIRE(argv != NULL && *argv == NULL);
@@ -1383,14 +1383,13 @@ dns_zone_getdbtype(dns_zone_t *zone, char ***argv, isc_mem_t *mctx) {
                size += strlen(zone->db_argv[i]) + 1;
        mem = isc_mem_allocate(mctx, size);
        if (mem != NULL) {
-               size_t remaining;
                tmp = mem;
                tmp2 = mem;
+               base = mem;
                tmp2 += (zone->db_argc + 1) * sizeof(char *);
                for (i = 0; i < zone->db_argc; i++) {
-                       remaining = size - (tmp2 - (char *) mem);
                        *tmp++ = tmp2;
-                       strlcpy(tmp2, zone->db_argv[i], remaining);
+                       strlcpy(tmp2, zone->db_argv[i], size - (tmp2 - base));
                        tmp2 += strlen(tmp2) + 1;
                }
                *tmp = NULL;
index 2b72c22af31fff225984e55693c1a9ad564e92c4..b455666969846f4b1911d66e7dd3e0554b86ea24 100644 (file)
@@ -374,8 +374,7 @@ getaddrinfo(const char *hostname, const char *servname,
                 */
                ntmp[0] = '\0';
                if (strchr(hostname, '%') != NULL) {
-                       strncpy(ntmp, hostname, sizeof(ntmp) - 1);
-                       ntmp[sizeof(ntmp) - 1] = '\0';
+                       strlcpy(ntmp, hostname, sizeof(ntmp));
                        p = strchr(ntmp, '%');
                        ep = NULL;
 
index 6de96efbefa6304dba346196c103665942f470bd..9d421b86c565e2fd659e55d872997d1f0dea7a12 100644 (file)
@@ -1671,12 +1671,12 @@ isc___mem_strdup(isc_mem_t *mctx0, const char *s FLARG) {
        REQUIRE(VALID_CONTEXT(mctx));
        REQUIRE(s != NULL);
 
-       len = strlen(s);
+       len = strlen(s) + 1;
 
-       ns = isc__mem_allocate((isc_mem_t *)mctx, len + 1 FLARG_PASS);
+       ns = isc__mem_allocate((isc_mem_t *)mctx, len FLARG_PASS);
 
        if (ns != NULL)
-               strncpy(ns, s, len + 1);
+               strlcpy(ns, s, len);
 
        return (ns);
 }
@@ -1827,8 +1827,7 @@ isc_mem_setname(isc_mem_t *ctx0, const char *name, void *tag) {
        REQUIRE(VALID_CONTEXT(ctx));
 
        LOCK(&ctx->lock);
-       memset(ctx->name, 0, sizeof(ctx->name));
-       strncpy(ctx->name, name, sizeof(ctx->name) - 1);
+       strlcpy(ctx->name, name, sizeof(ctx->name));
        ctx->tag = tag;
        UNLOCK(&ctx->lock);
 }
@@ -1913,8 +1912,7 @@ isc__mempool_setname(isc_mempool_t *mpctx0, const char *name) {
        if (mpctx->lock != NULL)
                LOCK(mpctx->lock);
 
-       strncpy(mpctx->name, name, sizeof(mpctx->name) - 1);
-       mpctx->name[sizeof(mpctx->name) - 1] = '\0';
+       strlcpy(mpctx->name, name, sizeof(mpctx->name));
 
        if (mpctx->lock != NULL)
                UNLOCK(mpctx->lock);
index a7347d1d2d0fe72ccba6cca23dcab0144708c3d1..00e8f485847ff32b9569e76e38df3a4ba77e1435 100644 (file)
@@ -408,9 +408,13 @@ pk11_get_session(pk11_context_t *ctx, pk11_optype_t optype,
        /* Override the token's PIN */
        if (logon && pin != NULL && *pin != '\0') {
                if (strlen(pin) > PINLEN)
-                       return ISC_R_RANGE;
-               memset(token->pin, 0, PINLEN + 1);
-               strncpy(token->pin, pin, PINLEN);
+                       return (ISC_R_RANGE);
+               /*
+                * We want to zero out the old pin before
+                * overwriting with a new one.
+                */
+               memset(token->pin, 0, sizeof(token->pin));
+               strlcpy(token->pin, pin, sizeof(token->pin));
        }
 
        freelist = &token->sessions;
index c2a371faefbea1f901edef20f63fba43a8f9871d..7d2857f22805813eb8f2ed554333967a66d47539 100644 (file)
@@ -882,8 +882,7 @@ isc__task_setname(isc_task_t *task0, const char *name, void *tag) {
        REQUIRE(VALID_TASK(task));
 
        LOCK(&task->lock);
-       memset(task->name, 0, sizeof(task->name));
-       strncpy(task->name, name, sizeof(task->name) - 1);
+       strlcpy(task->name, name, sizeof(task->name));
        task->tag = tag;
        UNLOCK(&task->lock);
 }
index 6d31eb74d8d6683187b8deb1426a52ff0ccf7ffa..841b070f59db8d81ccd8e7454e941c88bbad2de2 100644 (file)
@@ -234,11 +234,12 @@ isc_file_template(const char *path, const char *templet, char *buf,
        s = strrchr(path, '/');
 
        if (s != NULL) {
-               if ((s - path + 1 + strlen(templet) + 1) > buflen)
+               size_t prefixlen = s - path + 1;
+               if ((prefixlen + strlen(templet) + 1) > buflen)
                        return (ISC_R_NOSPACE);
 
-               strncpy(buf, path, s - path + 1);
-               buf[s - path + 1] = '\0';
+               /* Copy 'prefixlen' bytes and NUL terminate. */
+               strlcpy(buf, path, ISC_MIN(prefixlen + 1, buflen));
                strlcat(buf, templet, buflen);
        } else {
                if ((strlen(templet) + 1) > buflen)
index 1d24b842b7d2049d1c29452b9ddd6aa7e9fa1df5..330545ae0c8e58c6d05932b5d6adb8251daa64ee 100644 (file)
@@ -229,7 +229,7 @@ linux_if_inet6_current(isc_interfaceiter_t *iter) {
                }
        }
        isc_netaddr_fromin6(&iter->current.netmask, &addr6);
-       strncpy(iter->current.name, name, sizeof(iter->current.name));
+       strlcpy(iter->current.name, name, sizeof(iter->current.name));
        return (ISC_R_SUCCESS);
 }
 #endif
index 61b598aa50337e5623bc619b6ed41cea16c111ad..0f6ea564f610d590fd71f845980ba13555acd472 100644 (file)
@@ -5634,7 +5634,7 @@ isc__socket_filter(isc_socket_t *sock0, const char *filter) {
 
 #if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER)
        bzero(&afa, sizeof(afa));
-       strncpy(afa.af_name, filter, sizeof(afa.af_name));
+       strlcpy(afa.af_name, filter, sizeof(afa.af_name));
        if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER,
                         &afa, sizeof(afa)) == -1) {
                isc__strerror(errno, strbuf, sizeof(strbuf));
@@ -6519,8 +6519,7 @@ isc__socket_setname(isc_socket_t *socket0, const char *name, void *tag) {
        REQUIRE(VALID_SOCKET(sock));
 
        LOCK(&sock->lock);
-       memset(sock->name, 0, sizeof(sock->name));
-       strncpy(sock->name, name, sizeof(sock->name) - 1);
+       strlcpy(sock->name, name, sizeof(sock->name));
        sock->tag = tag;
        UNLOCK(&sock->lock);
 }
index 49aa967e9708f736a818f4e9de8b2a53f7955b80..71bf8504cada0641c82bde91f9528a319be0c51e 100644 (file)
@@ -394,8 +394,7 @@ isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
                snprintf(buf + flen, len - flen,
                         ".%03u", t->nanoseconds / NS_PER_MS);
        else {
-               strncpy(buf, "99-Bad-9999 99:99:99.999", len);
-               buf[len - 1] = 0;
+               strlcpy(buf, "99-Bad-9999 99:99:99.999", len);
        }
 }
 
index 2f81f8a69bcee9818d85359685bd9373599bf780..fce055a6b4c8d828b2235636ffbafc541c8bfaf9 100644 (file)
@@ -334,11 +334,12 @@ isc_file_template(const char *path, const char *templet, char *buf,
        s = strrchr(path, '\\');
 
        if (s != NULL) {
-         if ((s - path + 1 + strlen(templet) + 1) > (ssize_t)buflen)
+               size_t prefixlen = s - path + 1;
+               if ((prefixlen + strlen(templet) + 1) > (ssize_t)buflen)
                        return (ISC_R_NOSPACE);
 
-               strncpy(buf, path, s - path + 1);
-               buf[s - path + 1] = '\0';
+               /* Copy 'prefixlen' bytes and NUL terminate. */
+               strlcpy(buf, path, ISC_MIN(prefixlen + 1, buflen));
                strlcat(buf, templet, buflen);
        } else {
                if ((strlen(templet) + 1) > buflen)
@@ -616,8 +617,8 @@ isc_file_progname(const char *filename, char *progname, size_t namelen) {
        if (len >= namelen)
                return (ISC_R_NOSPACE);
 
-       strlcpy(progname, s, len);
-       progname[len] = '\0';
+       /* Copy up to 'len' bytes and NUL terminate. */
+       strlcpy(progname, s, ISC_MIN(len + 1, namelen));
        return (ISC_R_SUCCESS);
 }
 
index ffd6e23ab5bafcba4976200d906ea1c5fbb83e4e..de3e6c9e0085a59e1f85e69f8c5d645650fa0338 100644 (file)
@@ -70,11 +70,9 @@ is_ntfs(const char * file) {
         */
        if (isalpha(filename[0]) && filename[1] == ':' &&
                (filename[2] == '\\' || filename[2] == '/')) {
-               strncpy(drive, filename, 3);
-               drive[3] = '\0';
-       }
-
-       else if ((filename[0] == '\\') && (filename[1] == '\\')) {
+               /* Copy 'c:\' or 'c:/' and NUL terminate. */
+               strlcpy(drive, filename, ISC_MIN(3 + 1, sizeof(drive)));
+       } else if ((filename[0] == '\\') && (filename[1] == '\\')) {
                /* Find the machine and share name and rebuild the UNC */
                strlcpy(tmpbuf, filename, sizeof(tmpbuf));
                machinename = strtok(tmpbuf, "\\");
@@ -85,13 +83,12 @@ is_ntfs(const char * file) {
                strlcat(drive, sharename, sizeof(drive));
                strlcat(drive, "\\", sizeof(drive));
 
-       }
-       else /* Not determinable */
+       } else /* Not determinable */
                return (FALSE);
 
        GetVolumeInformation(drive, NULL, 0, NULL, 0, NULL, FSType,
                             sizeof(FSType));
-       if(strcmp(FSType,"NTFS") == 0)
+       if (strcmp(FSType, "NTFS") == 0)
                return (TRUE);
        else
                return (FALSE);
index 5c08599280d1834d5fdf294b57885e1014f8885f..08cd8953093b5fac01bb503ab0e2bd01c7985911 100644 (file)
@@ -46,7 +46,7 @@ isc_ntpaths_init(void) {
        HKEY hKey;
        BOOL keyFound = TRUE;
 
-       memset(namedBase, 0, MAX_PATH);
+       memset(namedBase, 0, sizeof(namedBase));
        if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, BIND_SUBKEY, 0, KEY_READ, &hKey)
                != ERROR_SUCCESS)
                keyFound = FALSE;
@@ -61,33 +61,35 @@ isc_ntpaths_init(void) {
 
        GetSystemDirectory(systemDir, MAX_PATH);
 
-       if (keyFound == FALSE)
+       if (keyFound == FALSE) {
                /* Use the System Directory as a default */
-               strcpy(namedBase, systemDir);
+               strlcpy(namedBase, systemDir, sizeof(namedBase));
+       }
 
-       strcpy(ns_confFile, namedBase);
-       strcat(ns_confFile, "\\etc\\named.conf");
+       strlcpy(ns_confFile, namedBase, sizeof(ns_confFile));
+       strlcat(ns_confFile, "\\etc\\named.conf", sizeof(ns_confFile));
 
-       strcpy(rndc_keyFile, namedBase);
-       strcat(rndc_keyFile, "\\etc\\rndc.key");
+       strlcpy(rndc_keyFile, namedBase, sizeof(rndc_keyFile));
+       strlcat(rndc_keyFile, "\\etc\\rndc.key", sizeof(rndc_keyFile));
 
-       strcpy(session_keyFile, namedBase);
-       strcat(session_keyFile, "\\etc\\session.key");
+       strlcpy(session_keyFile, namedBase, sizeof(session_keyFile));
+       strlcat(session_keyFile, "\\etc\\session.key", sizeof(session_keyFile));
 
-       strcpy(rndc_confFile, namedBase);
-       strcat(rndc_confFile, "\\etc\\rndc.conf");
+       strlcpy(rndc_confFile, namedBase, sizeof(rndc_confFile));
+       strlcat(rndc_confFile, "\\etc\\rndc.conf", sizeof(rndc_confFile));
 
-       strcpy(ns_defaultpidfile, namedBase);
-       strcat(ns_defaultpidfile, "\\etc\\named.pid");
+       strlcpy(ns_defaultpidfile, namedBase, sizeof(ns_defaultpidfile));
+       strlcat(ns_defaultpidfile, "\\etc\\named.pid",
+               sizeof(ns_defaultpidfile));
 
-       strcpy(ns_lockfile, namedBase);
-       strcat(ns_lockfile, "\\etc\\named.lock");
+       strlcpy(ns_lockfile, namedBase, sizeof(ns_lockfile));
+       strlcat(ns_lockfile, "\\etc\\named.lock", sizeof(ns_lockfile));
 
-       strcpy(local_state_dir, namedBase);
-       strcat(local_state_dir, "\\bin");
+       strlcpy(local_state_dir, namedBase, sizeof(local_state_dir));
+       strlcat(local_state_dir, "\\bin", sizeof(local_state_dir));
 
-       strcpy(sys_conf_dir, namedBase);
-       strcat(sys_conf_dir, "\\etc");
+       strlcpy(sys_conf_dir, namedBase, sizeof(sys_conf_dir));
+       strlcat(sys_conf_dir, "\\etc", sizeof(sys_conf_dir));
 
        /* Added to avoid an assert on NULL value */
        strlcpy(resolv_confFile, namedBase, sizeof(resolv_confFile));
index eb4c2e6c6b9650f30c8327791d7881834a55c25e..fa752dc1b07c560d061f1286211b9494b352d602 100644 (file)
@@ -3946,8 +3946,7 @@ isc__socket_setname(isc_socket_t *socket, const char *name, void *tag) {
        REQUIRE(VALID_SOCKET(socket));
 
        LOCK(&socket->lock);
-       memset(socket->name, 0, sizeof(socket->name));
-       strncpy(socket->name, name, sizeof(socket->name) - 1);
+       strlcpy(socket->name, name, sizeof(socket->name));
        socket->tag = tag;
        UNLOCK(&socket->lock);
 }
index 037063bc15c61a8e9ba0808d18a782d6845235ab..fe41d85cf0f2585df5545db3ebc706189a651414 100644 (file)
@@ -283,8 +283,7 @@ isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
                         st.wMilliseconds);
 
        } else {
-               strncpy(buf, "99-Bad-9999 99:99:99.999", len);
-               buf[len - 1] = 0;
+               strlcpy(buf, "99-Bad-9999 99:99:99.999", len);
        }
 }
 
index 461692ba1c11f57115673123983c13fb0c6f909c..be819a7d6ee49196a4e199c2035d9e8d1e87aa2c 100644 (file)
@@ -395,8 +395,7 @@ ns_interface_create(ns_interfacemgr_t *mgr, isc_sockaddr_t *addr,
        ifp->generation = mgr->generation;
        ifp->addr = *addr;
        ifp->flags = 0;
-       strncpy(ifp->name, name, sizeof(ifp->name));
-       ifp->name[sizeof(ifp->name)-1] = '\0';
+       strlcpy(ifp->name, name, sizeof(ifp->name));
        ifp->clientmgr = NULL;
 
        result = isc_mutex_init(&ifp->lock);