From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x35.google.com (mail-oa1-x35.google.com [IPv6:2001:4860:4864:20::35]) by sourceware.org (Postfix) with ESMTPS id A136C3856DF4 for ; Fri, 6 May 2022 13:40:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A136C3856DF4 Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-ee1e7362caso3105223fac.10 for ; Fri, 06 May 2022 06:40:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=O+9YC8MmcDKcl4YI3j9NmPJth/VBcHC6oWkVjO/x2B0=; b=cDSsTPDLfJ9yjt+M/QCKzG61h8Uma+VIutPUsuv/hyTLfk69+FJlgmspF3OIs2XxZU 38jW6MVfwp6kFTJo8pXWzMjgQk+jJAXg56cjv2vM49SSN8pCosOt4AFTh9O90oPH7tD0 kbhyzVo/WQsj20CvuW7hUI2UXmJ+7qpduwTh+2PwWwPZI6zadyZtmAhjYKZsz3EfIjye nZu8OMPr9rZIYR1YmUQqzwCkTcV8zVP28tnfhdr1L60TYriw/1Y0xS6SyaezZ7p5r0qh Hrfy3kM70bBvCfldFsDXgnA66qvNxiuFWA92ERRovBKOvGN/ySc5cxgNNKr+5nAUAfl2 CfRw== X-Gm-Message-State: AOAM530IFyC30OTT85lQEdGvgJQSiytnCn8A5qE/+lrY/OjOXU4OKNa8 noRSQwDqYreg1NUv84Rzd/h3tiYzA+FP2w== X-Google-Smtp-Source: ABdhPJxtyWzC0bkHYiOChWSVOTTWal3oRIM94ZQAAkhSujwRlpZsE9WD0ESkcgURFYYkQ6FQVa++FQ== X-Received: by 2002:a05:6870:471f:b0:ed:a180:77e8 with SMTP id b31-20020a056870471f00b000eda18077e8mr1202046oaq.19.1651844420271; Fri, 06 May 2022 06:40:20 -0700 (PDT) Received: from ?IPV6:2804:431:c7cb:726:53f7:4ba7:4810:79df? ([2804:431:c7cb:726:53f7:4ba7:4810:79df]) by smtp.gmail.com with ESMTPSA id s4-20020a4aa544000000b0035eb4e5a6c0sm1913265oom.22.2022.05.06.06.40.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 06 May 2022 06:40:19 -0700 (PDT) Message-ID: <5ccd428e-698f-1b57-e59c-85dbb8f0c114@linaro.org> Date: Fri, 6 May 2022 10:40:16 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH 2/2] wcrtomb: Make behavior POSIX compliant Content-Language: en-US To: Paul Eggert , Siddhesh Poyarekar , libc-alpha@sourceware.org Cc: fweimer@redhat.com, jakub@redhat.com, schwab@linux-m68k.org, dickey@his.com References: <20220505184348.3357550-1-siddhesh@sourceware.org> <20220505184348.3357550-3-siddhesh@sourceware.org> <804563bc-8ed8-9526-e2da-ab331d98e578@cs.ucla.edu> From: Adhemerval Zanella In-Reply-To: <804563bc-8ed8-9526-e2da-ab331d98e578@cs.ucla.edu> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.3 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , X-List-Received-Date: Fri, 06 May 2022 13:40:22 -0000 On 06/05/2022 06:25, Paul Eggert wrote: > On 5/5/22 11:43, Siddhesh Poyarekar wrote: >> +      else if (__glibc_unlikely (result > 1)) >> +    *((uint16_t *) s) = *((uint16_t *) buf); > > Shouldn't this be protected by "#if _STRING_ARCH_unaligned"? Also, unnecessary parens. I think this is an aliasing violation and most likely will fault on architectures that do not support unaligned access (such as sparc and some arm environments). > > But better yet, just call memcpy as the compiler will figure it out; see below. > > >> -    result = data.__outbuf - (unsigned char *) s; >> +    result = data.__outbuf - (unsigned char *) buf; >>    else >>      { >>        result = (size_t) -1; >>        __set_errno (EILSEQ); >>      } >>   >> +  if (result != (size_t) -1 && s != NULL) > > The 'result != (size_t) -1' can be omitted if you move that 'if' into the previous if's then-part. > > >> +  data.__outbufend = (unsigned char *) buf + MB_CUR_MAX; > > This'd be a bit faster (and less confusing) if we replace 'MB_CUR_MAX' with 'sizeof buf'. > > >> +      if (__glibc_unlikely (result > 2)) >> +    memcpy (s, buf, result); >> +      else if (__glibc_unlikely (result > 1)) >> +    *((uint16_t *) s) = *((uint16_t *) buf); >> +      else >> +    *s = *buf; > > If the likely path is result == 1, shouldn't that be checked first? Something like this: > >      if (__glibc_likely (result < 2)) >        *s = *buf; >      else if (__glibc_likely (result == 2)) >        memcpy (s, buf, result); /* Help the compiler.  */ >      else >        memcpy (s, buf, result);