From patchwork Thu Oct 1 14:17:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 40576 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C8459398B431; Thu, 1 Oct 2020 14:17:55 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id B856F398B431 for ; Thu, 1 Oct 2020 14:17:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B856F398B431 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=schwab@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 6AE5BB56C for ; Thu, 1 Oct 2020 14:17:51 +0000 (UTC) From: Andreas Schwab To: libc-alpha@sourceware.org Subject: [PATCH] __vfscanf_internal: fix aliasing violation (bug 26690) X-Yow: All I can think of is a platter of organic PRUNE CRISPS being trampled by an army of swarthy, Italian LOUNGE SINGERS... Date: Thu, 01 Oct 2020 16:17:49 +0200 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" As noted in , the cast in the call to the read_int function is an aliasing violation. Change the type of local variable f to a pointer to unsigned, which allows to eliminate most casts while only adding three new ones. --- stdio-common/vfscanf-internal.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/stdio-common/vfscanf-internal.c b/stdio-common/vfscanf-internal.c index 95b46dcbeb..3a323547f9 100644 --- a/stdio-common/vfscanf-internal.c +++ b/stdio-common/vfscanf-internal.c @@ -277,7 +277,7 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, #endif { va_list arg; - const CHAR_T *f = format; + const UCHAR_T *f = (const UCHAR_T *) format; UCHAR_T fc; /* Current character of the format. */ WINT_T done = 0; /* Assignments done. */ size_t read_in = 0; /* Chars read in. */ @@ -415,10 +415,11 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, #endif #ifndef COMPILE_WSCANF - if (!isascii ((unsigned char) *f)) + if (!isascii (*f)) { /* Non-ASCII, may be a multibyte. */ - int len = __mbrlen (f, strlen (f), &state); + int len = __mbrlen ((const char *) f, strlen ((const char *) f), + &state); if (len > 0) { do @@ -426,7 +427,7 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, c = inchar (); if (__glibc_unlikely (c == EOF)) input_error (); - else if (c != (unsigned char) *f++) + else if (c != *f++) { ungetc_not_eof (c, s); conv_error (); @@ -484,9 +485,9 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, char_buffer_rewind (&charbuf); /* Check for a positional parameter specification. */ - if (ISDIGIT ((UCHAR_T) *f)) + if (ISDIGIT (*f)) { - argpos = read_int ((const UCHAR_T **) &f); + argpos = read_int (&f); if (*f == L_('$')) ++f; else @@ -521,8 +522,8 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, /* Find the maximum field width. */ width = 0; - if (ISDIGIT ((UCHAR_T) *f)) - width = read_int ((const UCHAR_T **) &f); + if (ISDIGIT (*f)) + width = read_int (&f); got_width: if (width == 0) width = -1; @@ -2522,12 +2523,11 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, } while ((fc = *f++) != '\0' && fc != ']') - if (fc == '-' && *f != '\0' && *f != ']' - && (unsigned char) f[-2] <= (unsigned char) *f) + if (fc == '-' && *f != '\0' && *f != ']' && f[-2] <= *f) { /* Add all characters from the one before the '-' up to (but not including) the next format char. */ - for (fc = (unsigned char) f[-2]; fc < (unsigned char) *f; ++fc) + for (fc = f[-2]; fc < *f; ++fc) ((char *)charbuf.scratch.data)[fc] = 1; } else