From patchwork Tue Dec 10 21:15:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Adhemerval Zanella (Code Review)" X-Patchwork-Id: 1207289 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-107933-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gnutoolchain-gerrit.osci.io Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="uUI+4CkD"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47XXsj5lWNz9sPJ for ; Wed, 11 Dec 2019 08:15:53 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:cc:subject:in-reply-to:references :reply-to:mime-version:content-transfer-encoding:content-type :message-id; q=dns; s=default; b=LiKcYgRYC/yuYvfltAvTHxkZLKuXmbH CPiO1lX/cyhmj1UtWvmWUWYJWCm6qGut+QYhu6iwNS/KjuqH7oyWKsM5egUozdFF gqyQWDvBQC1r8SR0SVAb/A8pcFlVFpZ2JXSM6fLVo9zIb6usBTTm68UpTOZ/znos jE6VCGthu/4s= 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:date:from:to:cc:subject:in-reply-to:references :reply-to:mime-version:content-transfer-encoding:content-type :message-id; s=default; bh=BG1FCVNDqUR9c7WM10Q7SnZjZFI=; b=uUI+4 CkDB/SqSP8wC7FgjUgtYo6WvP+1uRAH4K9DkmIKsC0veNbbo7T1WDSvoLrCC2Y6E ZbgyJhe9MGlNdpmI1KBC7bbAXtAo46lcNuyL/Yh8D2ssCs1kZNy+AhdnppDNRr+K CSuRX97ssRmqZHYv9Tpb3BPNHYvOdzx1jdYoi8= Received: (qmail 82214 invoked by alias); 10 Dec 2019 21:15:46 -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 82112 invoked by uid 89); 10 Dec 2019 21:15:41 -0000 Authentication-Results: sourceware.org; auth=none 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, KAM_SHORT autolearn=ham version=3.3.1 spammy= X-HELO: mx1.osci.io X-Gerrit-PatchSet: 4 Date: Tue, 10 Dec 2019 16:15:22 -0500 From: "Carlos O'Donell (Code Review)" To: Florian Weimer , libc-alpha@sourceware.org Cc: Simon Marchi Auto-Submitted: auto-generated X-Gerrit-MessageType: newpatchset Subject: [review v4] localedef: Add verbose messages for failure paths. X-Gerrit-Change-Id: I28b9f680711ff00252a2cb15625b774cc58ecb9d X-Gerrit-Change-Number: 303 X-Gerrit-ChangeURL: X-Gerrit-Commit: 4ebdb1905403be89ac0557a1cae2cf4119daf595 In-Reply-To: References: Reply-To: carlos@redhat.com, simon.marchi@polymtl.ca, fweimer@redhat.com, libc-alpha@sourceware.org MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/3.0.3-79-g83ff7f88f1 Message-Id: <20191210211525.2415420AF6@gnutoolchain-gerrit.osci.io> Change URL: https://gnutoolchain-gerrit.osci.io/r/c/glibc/+/303 ...................................................................... localedef: Add verbose messages for failure paths. During testing of localedef running in a minimal container there were several error cases which were hard to diagnose since they appeared as strerror (errno) values printed by the higher level functions. This change adds three new verbose messages for potential failure paths. The new messages give the user the opportunity to use -v and display additional information about why localedef might be failing. I found these messages useful myself while writing a localedef container test for --no-hard-links. Change-Id: I28b9f680711ff00252a2cb15625b774cc58ecb9d --- A include/programs/xasprintf.h M locale/Makefile M locale/programs/localedef.c M locale/programs/localedef.h A locale/programs/xasprintf.c 5 files changed, 120 insertions(+), 52 deletions(-) diff --git a/include/programs/xasprintf.h b/include/programs/xasprintf.h new file mode 100644 index 0000000..53193ba --- /dev/null +++ b/include/programs/xasprintf.h @@ -0,0 +1,24 @@ +/* asprintf with out of memory checking + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . */ + +#ifndef _XASPRINTF_H +#define _XASPRINTF_H 1 + +extern char *xasprintf (const char *format, ...) + __attribute__ ((__format__ (__printf__, 1, 2), __warn_unused_result__)); + +#endif /* xasprintf.h */ diff --git a/locale/Makefile b/locale/Makefile index 1a19b6f..943df35 100644 --- a/locale/Makefile +++ b/locale/Makefile @@ -56,7 +56,7 @@ localedef-aux := md5 locale-modules := locale locale-spec lib-modules := charmap-dir simple-hash xmalloc xstrdup \ - record-status + record-status xasprintf GPERF = gperf diff --git a/locale/programs/localedef.c b/locale/programs/localedef.c index 3dcf15f..b926117 100644 --- a/locale/programs/localedef.c +++ b/locale/programs/localedef.c @@ -434,20 +434,16 @@ { case ARGP_KEY_HELP_EXTRA: /* We print some extra information. */ - if (asprintf (&tp, gettext ("\ + tp = xasprintf (gettext ("\ For bug reporting instructions, please see:\n\ -%s.\n"), REPORT_BUGS_TO) < 0) - return NULL; - if (asprintf (&cp, gettext ("\ +%s.\n"), REPORT_BUGS_TO); + cp = xasprintf (gettext ("\ System's directory for character maps : %s\n\ repertoire maps: %s\n\ locale path : %s\n\ %s"), - CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, tp) < 0) - { - free (tp); - return NULL; - } + CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, tp); + free (tp); return cp; default: break; @@ -477,15 +473,13 @@ } -/* The parameter to localedef describes the output path. If it does - contain a '/' character it is a relative path. Otherwise it names the - locale this definition is for. */ +/* The parameter to localedef describes the output path. If it does contain a + '/' character it is a relative path. Otherwise it names the locale this + definition is for. The returned path must be freed by the caller. */ static const char * construct_output_path (char *path) { - const char *normal = NULL; char *result; - char *endp; if (strchr (path, '/') == NULL) { @@ -493,50 +487,44 @@ contains a reference to the codeset. This should be normalized. */ char *startp; + char *endp = NULL; + const char *normal = NULL; startp = path; - /* We must be prepared for finding a CEN name or a location of - the introducing `.' where it is not possible anymore. */ + /* Either we have a '@' which starts a CEN name or '.' which starts the + codeset specification. The CEN name starts with '@' and may also have + a codeset specification, but we do not normalize the string after '@'. + If we only find the codeset specification then we normalize only the codeset + specification (but not anything after a subsequent '@'). */ while (*startp != '\0' && *startp != '@' && *startp != '.') ++startp; if (*startp == '.') { /* We found a codeset specification. Now find the end. */ endp = ++startp; + + /* Stop at the first '@', and don't normalize anything past that. */ while (*endp != '\0' && *endp != '@') ++endp; if (endp > startp) normal = normalize_codeset (startp, endp - startp); } - else - /* This is to keep gcc quiet. */ - endp = NULL; - /* We put an additional '\0' at the end of the string because at - the end of the function we need another byte for the trailing - '/'. */ - ssize_t n; if (normal == NULL) - n = asprintf (&result, "%s%s/%s%c", output_prefix ?: "", - COMPLOCALEDIR, path, '\0'); + result = xasprintf ("%s%s/%s/", output_prefix ?: "", + COMPLOCALEDIR, path); else - n = asprintf (&result, "%s%s/%.*s%s%s%c", - output_prefix ?: "", COMPLOCALEDIR, - (int) (startp - path), path, normal, endp, '\0'); - - if (n < 0) - return NULL; - - endp = result + n - 1; + result = xasprintf ("%s%s/%.*s%s%s/", + output_prefix ?: "", COMPLOCALEDIR, + (int) (startp - path), path, normal, endp ?: ""); + /* Free the allocated normalized codeset name. */ + free ((char *) normal); } else { - /* This is a user path. Please note the additional byte in the - memory allocation. */ - size_t len = strlen (path) + 1; - result = xmalloc (len + 1); - endp = mempcpy (result, path, len) - 1; + /* This is a user path. */ + result = xasprintf ("%s/", path); /* If the user specified an output path we cannot add the output to the archive. */ @@ -546,24 +534,40 @@ errno = 0; if (no_archive && euidaccess (result, W_OK) == -1) - /* Perhaps the directory does not exist now. Try to create it. */ - if (errno == ENOENT) - { - errno = 0; - if (mkdir (result, 0777) < 0) - return NULL; - } - - *endp++ = '/'; - *endp = '\0'; + { + /* Perhaps the directory does not exist now. Try to create it. */ + if (errno == ENOENT) + { + errno = 0; + if (mkdir (result, 0777) < 0) + { + record_verbose (stderr, + _("cannot create output path \"%s\": %s"), + result, strerror (errno)); + free (result); + return NULL; + } + } + else + record_verbose (stderr, + _("no write permission to output path \"%s\": %s"), + result, strerror (errno)); + } return result; } -/* Normalize codeset name. There is no standard for the codeset - names. Normalization allows the user to use any of the common - names. */ +/* Normalize codeset name. There is no standard for the codeset names. + Normalization allows the user to use any of the common names e.g. UTF-8, + utf-8, utf8, UTF8 etc. + + We normalize using the following rules: + - Remove all non-alpha-numeric characters + - Lowercase all cahracters. + - If there are only digits assume it's an ISO standard and prefix with 'iso' + + We return the normalized string which needs to be freed by free. */ static const char * normalize_codeset (const char *codeset, size_t name_len) { @@ -573,6 +577,7 @@ char *wp; size_t cnt; + /* Compute the length of only the alpha-numeric characters. */ for (cnt = 0; cnt < name_len; ++cnt) if (isalnum (codeset[cnt])) { @@ -582,6 +587,8 @@ only_digit = 0; } + /* If there were only digits we assume it's an ISO standard and we will + prefix with 'iso' so include space for that. */ retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1); if (retval != NULL) @@ -592,6 +599,7 @@ wp = retval; for (cnt = 0; cnt < name_len; ++cnt) + /* Lowercase all characters. */ if (isalpha (codeset[cnt])) *wp++ = tolower (codeset[cnt]); else if (isdigit (codeset[cnt])) @@ -600,6 +608,7 @@ *wp = '\0'; } + /* Return allocated and converted name for caller to free. */ return (const char *) retval; } diff --git a/locale/programs/localedef.h b/locale/programs/localedef.h index 80da0b0..ad2a927 100644 --- a/locale/programs/localedef.h +++ b/locale/programs/localedef.h @@ -123,6 +123,7 @@ /* Prototypes for a few program-wide used functions. */ #include +#include /* Mark given locale as to be read. */ diff --git a/locale/programs/xasprintf.c b/locale/programs/xasprintf.c new file mode 100644 index 0000000..efc91a9 --- /dev/null +++ b/locale/programs/xasprintf.c @@ -0,0 +1,34 @@ +/* asprintf with out of memory checking + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . */ + +#include +#include +#include +#include +#include + +char * +xasprintf (const char *format, ...) +{ + va_list ap; + va_start (ap, format); + char *result; + if (vasprintf (&result, format, ap) < 0) + error (EXIT_FAILURE, 0, _("memory exhausted")); + va_end (ap); + return result; +}