From owner-freebsd-audit Thu Jan 24 11:55:21 2002 Delivered-To: freebsd-audit@freebsd.org Received: from gate-internal.sim.ionidea.com (ion.so-com.net [212.110.132.83]) by hub.freebsd.org (Postfix) with ESMTP id 4FC3637B417; Thu, 24 Jan 2002 11:54:43 -0800 (PST) Received: (from phantom@localhost) by gate-internal.sim.ionidea.com (8.11.6/8.11.1) id g0OJwae58303; Thu, 24 Jan 2002 21:58:36 +0200 (EET) (envelope-from phantom) Date: Thu, 24 Jan 2002 21:58:35 +0200 From: Alexey Zelkin To: audit@freebsd.org, ache@freebsd.org Subject: CFR: strncpy -> strlcpy in setlocale() Message-ID: <20020124215835.A58294@gate.sim.ionidea.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.20i X-Operating-System: FreeBSD 4.2-RELEASE i386 Sender: owner-freebsd-audit@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG hi, Any objections against this patch ? It's based on rev 1.23 of NetBSD's setlocale.c. --- setlocale.c~ Thu Jan 24 18:13:03 2002 +++ setlocale.c Thu Jan 24 21:56:23 2002 @@ -132,33 +132,35 @@ if (!env || !*env || strchr(env, '/')) env = "C"; - (void) strncpy(new_categories[category], env, ENCODING_LEN); - new_categories[category][ENCODING_LEN] = '\0'; + (void)strlcpy(new_categories[category], env, + sizeof(new_categories[category])); if (category == LC_ALL) { for (i = 1; i < _LC_LAST; ++i) { if (!(env = getenv(categories[i])) || !*env) env = new_categories[LC_ALL]; - (void)strncpy(new_categories[i], env, ENCODING_LEN); - new_categories[i][ENCODING_LEN] = '\0'; + (void)strlcpy(new_categories[i], env, + sizeof(new_categories[category])); } } } else if (category != LC_ALL) { - (void)strncpy(new_categories[category], locale, ENCODING_LEN); - new_categories[category][ENCODING_LEN] = '\0'; + (void)strlcpy(new_categories[category], locale, + sizeof(new_categories[category])); } else { if ((r = strchr(locale, '/')) == NULL) { for (i = 1; i < _LC_LAST; ++i) { - (void)strncpy(new_categories[i], locale, ENCODING_LEN); - new_categories[i][ENCODING_LEN] = '\0'; + (void)strlcpy(new_categories[i], locale, + sizeof(new_categories[category])); } } else { - for (i = 1; r[1] == '/'; ++r); + for (i = 1; r[1] == '/'; ++r) + ; if (!r[1]) return (NULL); /* Hmm, just slashes... */ do { - len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale; - (void)strncpy(new_categories[i], locale, len); - new_categories[i][len] = '\0'; + len = r - locale > sizeof(new_categories[i]) + ? sizeof(new_categories[i]) + : r - locale; + (void)strlcpy(new_categories[i], locale, len); i++; locale = r; while (*locale == '/') @@ -167,7 +169,7 @@ } while (*locale); while (i < _LC_LAST) { (void)strcpy(new_categories[i], - new_categories[i-1]); + new_categories[i - 1]); i++; } } @@ -201,8 +203,8 @@ for (i = 2; i < _LC_LAST; ++i) if (strcmp(current_categories[1], current_categories[i])) { for (i = 2; i < _LC_LAST; ++i) { - (void) strcat(current_locale_string, "/"); - (void) strcat(current_locale_string, current_categories[i]); + (void)strcat(current_locale_string, "/"); + (void)strcat(current_locale_string, current_categories[i]); } break; } To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-audit" in the body of the message