From patchwork Tue Jun 20 19:12:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 106012 Delivered-To: patch@linaro.org Received: by 10.140.91.2 with SMTP id y2csp1556664qgd; Tue, 20 Jun 2017 12:12:26 -0700 (PDT) X-Received: by 10.98.19.145 with SMTP id 17mr32075950pft.208.1497985946810; Tue, 20 Jun 2017 12:12:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1497985946; cv=none; d=google.com; s=arc-20160816; b=ER/ACsEAFPOwm8RZpUClyGkyeUSuPt+Tpd02803zNdelW70aOW3oO3SnUEMa6cF6jG oWmNNUUTZWy8Yq7I0FYCUmg6OLWxi5FuWnpxgFMi3kJ8c5orVPqwHiNuYrUVrYL34DIj b9D/ZKw+2s+7JcJ2wlgIz3fKb4dh4sDUsZCDyxZ1P9y7nXekSree0ltmhQxDf9TLu5qS bCTiFeY1HLDbtS7T9tdc/Xq6RM9qrGExCvqhIKNGrTUQS5UeMZWy7ve4cPogrg5DYlcj x3TkiRUA1U37ItPW9S4eNAEB/IS5Lgitv0UW4IP9vbHvNWhL2ZkD/JXnirmibwORarrt 3SqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :delivered-to:sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=65P/FxIJnJukAuIZ7gIcVKcdqOy41/sCSJHy6WzUdo8=; b=ziMQIh5AAlqlBE6oy0j8P4ACYeF88KTCI0mWllyP2m4ywTbOSK7pRt9ECbeFDymZRF //Qx6NVsFTWLUVtW13coMbaEH72DfT9bSzG4Ar377/cHoC5LichHoYlArZgfC2klbwBK m36RCWgmeceAb8YOR9vG28LOVsS+1/6ZvWqeOY+BTcpaH5T0sBHyybFi9fjhPqwoBNwK eQ8NGUg2h4jh5RMFJT+kETPrCjE2Doh5JJPGjdtXBeRcKoFa25COPTu4LpEeJnkYRpgq rHXSDeoXoUiopyFIsR3rlV2GW0OsLQicGE0bcaxzQjhX3vDZ6PzjkGKY8dyIzI/Ul0pw SAlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.b=mV9Wgy8b; spf=pass (google.com: domain of libc-alpha-return-80841-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80841-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id t64si11286803pgd.567.2017.06.20.12.12.26 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Jun 2017 12:12:26 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80841-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.b=mV9Wgy8b; spf=pass (google.com: domain of libc-alpha-return-80841-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80841-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=F+zDUkzAtpxAL85UTYzneTjodgB3iTF olG0JE/qi3sNEtZ1QjSj8syF8fA3cMv4mH1etFf4BmfEtPPWmHVeFUFrLx7XDBwX KSWOfBC9+jKtHhp6kmcz9IglJlsRh1rn2XgL0Ub9fJk11TehWVIUw9KSFYqrjDD3 S1aw47sm9HJ4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=FWZbzVNTLJduLUZe8rWbDo+5CyE=; b=mV9Wg y8b7qtJPL8JIn1JAEOovMkmRnHh9TvR9arcHh1P/8edIC2y9oEVJkPHadW8GPoY6 Umjg4X7RQDJQtWOo/99G7DUaVzHFp+w7Kly7xlBt6sYUF8zJZnWx8dBqYPjFmRaS xc+XcGqVe3vpqVEIT+UQu79QVUYaNK9ErISxYU= Received: (qmail 123896 invoked by alias); 20 Jun 2017 19:12:15 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 123878 invoked by uid 89); 20 Jun 2017 19:12:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-ua0-f170.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=65P/FxIJnJukAuIZ7gIcVKcdqOy41/sCSJHy6WzUdo8=; b=e/0QKFQCPoIXU2Cfy/XNzyAXMQ6gENFlOWlEqgfXyCpvi2bcHRtxB185OTJPZipr77 f28gr34xZgtmLWS0apqS+Xes28LkLwdVVhrfsp7LG0v1J0jjMNyElfrh0KKjBjj+HLb1 E3BOT/DCeNYHRXBstol7E6kLOu6nOnTbAA5pR02iaFDluF5hx+Pqni5WiFvlQUdiNVTF xVd2CloiKe0GQUi5wG7Of9IRJI1flbfCcLSLZRvFg3T5PMHzOXrV0EXe5cD6tYRrBS9Q 6HBI1Z2xvpBNp7XrK2A1rZh7NozaXir4j/Mp5HtBZNr7jtXs1XMSvMPDBQ/ur4TW6eC1 Tz/w== X-Gm-Message-State: AKS2vOxx25zdturAEmClhnNG4zqS5J891OdYI281Ha91kXw5Z92l2red U6LnDeKxF0gcT2qZNqVoNw== X-Received: by 10.176.26.5 with SMTP id a5mr6529443uai.96.1497985930185; Tue, 20 Jun 2017 12:12:10 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Florian Weimer Subject: [PATCH 2/2] gconv: Replace norm_add_slashes with __gconv_norm_add_slashes Date: Tue, 20 Jun 2017 16:12:03 -0300 Message-Id: <1497985923-18986-2-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1497985923-18986-1-git-send-email-adhemerval.zanella@linaro.org> References: <1497985923-18986-1-git-send-email-adhemerval.zanella@linaro.org> From: Florian Weimer 2017-06-19 Florian Weimer Adhemerval Zanella * iconv/Makefile (routine): Add norm_add_slashes. * iconv/norm_add_slashes.c: New file, extracted from iconv/gconv_int.h. * iconv/gconv_int.h (norm_add_slashes): Remove. (__gconv_norm_add_slashes): Declare. * wcsmbs/wcsmbsload.c (__wcsmbs_load_conv): Use __gconv_norm_add_slashes. * intl/dcigettext.c (_nl_find_msg): Likewise. Simplify !_LIBC case. --- iconv/Makefile | 3 ++- iconv/gconv_int.h | 39 ++++++-------------------------- iconv/norm_add_slashes.c | 53 +++++++++++++++++++++++++++++++++++++++++++ intl/dcigettext.c | 59 ++++++++++++++++++++++++++---------------------- wcsmbs/wcsmbsload.c | 9 +++++--- 5 files changed, 100 insertions(+), 63 deletions(-) create mode 100644 iconv/norm_add_slashes.c -- 2.7.4 diff --git a/iconv/Makefile b/iconv/Makefile index b2fead0..df82b0d 100644 --- a/iconv/Makefile +++ b/iconv/Makefile @@ -25,7 +25,8 @@ include ../Makeconfig headers = iconv.h gconv.h routines = iconv_open iconv iconv_close \ gconv_open gconv gconv_close gconv_db gconv_conf \ - gconv_builtin gconv_simple gconv_trans gconv_cache + gconv_builtin gconv_simple gconv_trans gconv_cache \ + norm_add_slashes routines += gconv_dl vpath %.c ../locale/programs ../intl diff --git a/iconv/gconv_int.h b/iconv/gconv_int.h index 85a67ad..7a54e0f 100644 --- a/iconv/gconv_int.h +++ b/iconv/gconv_int.h @@ -121,38 +121,13 @@ extern const char *__gconv_path_envvar attribute_hidden; __libc_lock_define (extern, __gconv_lock attribute_hidden) -/* The gconv functions expects the name to be in upper case and complete, - including the trailing slashes if necessary. */ -#define norm_add_slashes(str,suffix) \ - ({ \ - const char *cp = (str); \ - char *result; \ - char *tmp; \ - size_t cnt = 0; \ - const size_t suffix_len = strlen (suffix); \ - \ - while (*cp != '\0') \ - if (*cp++ == '/') \ - ++cnt; \ - \ - tmp = result = __alloca (cp - (str) + 3 + suffix_len); \ - cp = (str); \ - while (*cp != '\0') \ - *tmp++ = __toupper_l (*cp++, _nl_C_locobj_ptr); \ - if (cnt < 2) \ - { \ - *tmp++ = '/'; \ - if (cnt < 1) \ - { \ - *tmp++ = '/'; \ - if (suffix_len != 0) \ - tmp = __mempcpy (tmp, suffix, suffix_len); \ - } \ - } \ - *tmp = '\0'; \ - result; \ - }) - +/* Convert NAME of NAME_LEN bytes to the form expected by the gconv + functions, including the trailing slashes if necessary. The caller + has to free the returned string. Return NULL on allocation + failure. */ +char *__gconv_norm_add_slashes (const char *name, size_t name_len, + const char *suffix) + attribute_hidden; /* Return in *HANDLE decriptor for transformation from FROMSET to TOSET. */ extern int __gconv_open (const char *toset, const char *fromset, diff --git a/iconv/norm_add_slashes.c b/iconv/norm_add_slashes.c new file mode 100644 index 0000000..4e7d09a --- /dev/null +++ b/iconv/norm_add_slashes.c @@ -0,0 +1,53 @@ +/* Normalize the charset name and add a suffix with slashes. + Copyright (C) 1997-2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#define CHAR_ARRAY_INITIAL_SIZE 0 +#include + +char * +__gconv_norm_add_slashes (const char *name, size_t name_len, + const char *suffix) +{ + size_t cnt = 0; + for (size_t i = 0; i < name_len; i++) + if (name[i] == '/') + cnt++; + + struct char_array result; + if (!char_array_init_str_size (&result, name, name_len)) + return NULL; + + for (size_t i = 0; i < char_array_length (&result); i++) + *char_array_at (&result, i) = __toupper_l (name[i], _nl_C_locobj_ptr); + + if (cnt < 2) + { + if (!char_array_append_str (&result, "/")) + return NULL; + if (cnt < 1) + { + if (!char_array_append_str (&result, "/") + | !char_array_append_str (&result, suffix)) + return NULL; + } + } + + return char_array_finalize (&result, NULL); +} diff --git a/intl/dcigettext.c b/intl/dcigettext.c index d97746c..49127d0 100644 --- a/intl/dcigettext.c +++ b/intl/dcigettext.c @@ -1123,25 +1123,24 @@ _nl_find_msg (struct loaded_l10nfile *domain_file, { size_t len; char *charset; - const char *outcharset; + char *outcharset; charsetstr += strlen ("charset="); len = strcspn (charsetstr, " \t\n"); - charset = (char *) alloca (len + 1); -# if defined _LIBC || HAVE_MEMPCPY - *((char *) mempcpy (charset, charsetstr, len)) = '\0'; -# else - memcpy (charset, charsetstr, len); - charset[len] = '\0'; -# endif - - outcharset = encoding; - # ifdef _LIBC /* We always want to use transliteration. */ - outcharset = norm_add_slashes (outcharset, "TRANSLIT"); - charset = norm_add_slashes (charset, ""); + charset = __gconv_norm_add_slashes (charsetstr, len, ""); + outcharset = __gconv_norm_add_slashes + (encoding, strlen (encoding), "TRANSLIT"); + if (charset == NULL || outcharset == NULL) + { + free ((char *) encoding); + free (outcharset); + free (charset); + goto unlock_fail; + } + int r = __gconv_open (outcharset, charset, &convd->conv, GCONV_AVOID_NOCONV); if (__builtin_expect (r != __GCONV_OK, 0)) @@ -1153,6 +1152,8 @@ _nl_find_msg (struct loaded_l10nfile *domain_file, { gl_rwlock_unlock (domain->conversions_lock); free ((char *) encoding); + free (outcharset); + free (charset); return NULL; } @@ -1165,27 +1166,31 @@ _nl_find_msg (struct loaded_l10nfile *domain_file, # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \ && !defined __UCLIBC__) \ || _LIBICONV_VERSION >= 0x0105 + charset = strndup (charsetstr, len); if (strchr (outcharset, '/') == NULL) { - char *tmp; - - len = strlen (outcharset); - tmp = (char *) alloca (len + 10 + 1); - memcpy (tmp, outcharset, len); - memcpy (tmp + len, "//TRANSLIT", 10 + 1); - outcharset = tmp; - - convd->conv = iconv_open (outcharset, charset); - - freea (outcharset); + if (asprintf (&outcharset, "%s//TRANSLIT", + encoding) < 0) + outcharset = NULL; } else + outcharset = strdup (encoding); + if (charset == NULL || outcharset == NULL) + { + gl_rwlock_unlock (domain->conversions_lock); + free (outcharset); + free (charset); + free ((char *) encoding); + return NULL; + } # endif - convd->conv = iconv_open (outcharset, charset); + convd->conv = iconv_open (outcharset, charset); # endif # endif - - freea (charset); + free (outcharset); + free (charset); + /* Do not free encoding here because + convd->encoding takes ownership. */ } } } diff --git a/wcsmbs/wcsmbsload.c b/wcsmbs/wcsmbsload.c index 656cc0a..cf7f815 100644 --- a/wcsmbs/wcsmbsload.c +++ b/wcsmbs/wcsmbsload.c @@ -160,7 +160,6 @@ __wcsmbs_load_conv (struct __locale_data *new_category) { /* We must find the real functions. */ const char *charset_name; - const char *complete_name; struct gconv_fcts *new_fcts; int use_translit; @@ -177,8 +176,11 @@ __wcsmbs_load_conv (struct __locale_data *new_category) /* Normalize the name and add the slashes necessary for a complete lookup. */ - complete_name = norm_add_slashes (charset_name, - use_translit ? "TRANSLIT" : ""); + char *complete_name = __gconv_norm_add_slashes + (charset_name, strlen (charset_name), + use_translit ? "TRANSLIT" : ""); + if (complete_name ==NULL) + goto failed; /* It is not necessary to use transliteration in this direction since the internal character set is supposed to be able to @@ -188,6 +190,7 @@ __wcsmbs_load_conv (struct __locale_data *new_category) if (new_fcts->towc != NULL) new_fcts->tomb = __wcsmbs_getfct (complete_name, "INTERNAL", &new_fcts->tomb_nsteps); + free (complete_name); /* If any of the conversion functions is not available we don't use any since this would mean we cannot convert back and