public inbox for libc-stable@sourceware.org
 help / color / mirror / Atom feed
* Re: [PATCH] string/stratcliff.c: Replace int with size_t [BZ #21982]
  2017-01-01  0:00                 ` [PATCH] string/stratcliff.c: Replace int with size_t [BZ #21982] H.J. Lu
@ 2017-01-01  0:00                   ` H.J. Lu
  0 siblings, 0 replies; 2+ messages in thread
From: H.J. Lu @ 2017-01-01  0:00 UTC (permalink / raw)
  To: Stefan Liebler, Libc-stable Mailing List; +Cc: GNU C Library

On Thu, Sep 7, 2017 at 9:20 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Wed, Aug 23, 2017 at 7:49 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> On Tue, Aug 22, 2017 at 7:56 AM, Stefan Liebler <stli@linux.vnet.ibm.com> wrote:
>>> On 08/22/2017 02:43 PM, H.J. Lu wrote:
>>>>
>>>> On Tue, Aug 22, 2017 at 5:07 AM, Stefan Liebler <stli@linux.vnet.ibm.com>
>>>> wrote:
>>>>>
>>>>> On 08/22/2017 01:05 AM, H.J. Lu wrote:
>>>>>>
>>>>>>
>>>>>> On Mon, Aug 21, 2017 at 8:41 AM, Stefan Liebler
>>>>>> <stli@linux.vnet.ibm.com>
>>>>>> wrote:
>>>>>>>
>>>>>>>
>>>>>>> On 08/21/2017 04:53 PM, H.J. Lu wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> On Mon, Aug 21, 2017 at 6:48 AM, Stefan Liebler
>>>>>>>> <stli@linux.vnet.ibm.com>
>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On 08/20/2017 07:17 PM, H.J. Lu wrote:
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Fix GCC 7 errors when string/stratcliff.c is compiled with -O3:
>>>>>>>>>>
>>>>>>>>>> stratcliff.c: In function ‘do_test’:
>>>>>>>>>> cc1: error: assuming signed overflow does not occur when assuming
>>>>>>>>>> that
>>>>>>>>>> (X
>>>>>>>>>> - c) <= X is always true [-Werror=strict-overflow]
>>>>>>>>>>
>>>>>>>>>> OK for master?
>>>>>>>>>>
>>>>>>>>>> H.J.
>>>>>>>>>> ---
>>>>>>>>>>            [BZ #21982]
>>>>>>>>>>            * string/stratcliff.c (do_test): Declare size, nchars,
>>>>>>>>>> inner,
>>>>>>>>>>            middle and outer with size_t instead of int.  Repleace %d
>>>>>>>>>> with
>>>>>>>>>>            %Zd in printf.
>>>>>>>>>> ---
>>>>>>>>>>      string/stratcliff.c | 72
>>>>>>>>>> ++++++++++++++++++++++++++---------------------------
>>>>>>>>>>      1 file changed, 36 insertions(+), 36 deletions(-)
>>>>>>>>>>
>>>>>>>>>> diff --git a/string/stratcliff.c b/string/stratcliff.c
>>>>>>>>>> index e28b0c5058..ae780379cb 100644
>>>>>>>>>> --- a/string/stratcliff.c
>>>>>>>>>> +++ b/string/stratcliff.c
>>>>>>>>>> @@ -58,8 +58,8 @@
>>>>>>>>>>      int
>>>>>>>>>>      do_test (void)
>>>>>>>>>>      {
>>>>>>>>>> -  int size = sysconf (_SC_PAGESIZE);
>>>>>>>>>> -  int nchars = size / sizeof (CHAR);
>>>>>>>>>> +  size_t size = sysconf (_SC_PAGESIZE);
>>>>>>>>>> +  size_t nchars = size / sizeof (CHAR);
>>>>>>>>>>        CHAR *adr;
>>>>>>>>>>        CHAR *dest;
>>>>>>>>>>        int result = 0;
>>>>>>>>>> @@ -80,7 +80,7 @@ do_test (void)
>>>>>>>>>>          }
>>>>>>>>>>        else
>>>>>>>>>>          {
>>>>>>>>>> -      int inner, middle, outer;
>>>>>>>>>> +      size_t inner, middle, outer;
>>>>>>>>>>
>>>>>>>>>>            mprotect (adr, size, PROT_NONE);
>>>>>>>>>>            mprotect (adr + 2 * nchars, size, PROT_NONE);
>>>>>>>>>> @@ -101,7 +101,7 @@ do_test (void)
>>>>>>>>>>
>>>>>>>>>>                  if (STRLEN (&adr[outer]) != (size_t) (inner -
>>>>>>>>>> outer))
>>>>>>>>>>                    {
>>>>>>>>>> -                 printf ("%s flunked for outer = %d, inner = %d\n",
>>>>>>>>>> +                 printf ("%s flunked for outer = %Zd, inner =
>>>>>>>>>> %Zd\n",
>>>>>>>>>>                              STRINGIFY (STRLEN), outer, inner);
>>>>>>>>>>                      result = 1;
>>>>>>>>>>                    }
>>>>>>>>>>                    {
>>>>>>>>>> -                 printf ("%s flunked for outer = %d, middle =
>>>>>>>>>> %d\n",
>>>>>>>>>> +                 printf ("%s flunked for outer = %Zd, middle =
>>>>>>>>>> %Zd\n",
>>>>>>>>>>                              STRINGIFY (rawmemchr), outer, middle);
>>>>>>>>>>                      result = 1;
>>>>>>>>>>                    }
>>>>>>>>>> Hi H.J. Lu,
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> I've applied your patch and the warnings does not occur anymore on
>>>>>>>>> s390.
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> Great.
>>>>>>>>
>>>>>>>>> The outer loops of the string tests are all using the following:
>>>>>>>>> size_t nchars, outer;
>>>>>>>>> for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
>>>>>>>>>
>>>>>>>>> I think we can assume, that nchars is always > 128 as it is derived
>>>>>>>>> by
>>>>>>>>> the
>>>>>>>>> pagesize.
>>>>>>>>> But if nchars would be equal to 128, this would result in an infinite
>>>>>>>>> loop
>>>>>>>>> (outer >= 0)?
>>>>>>>>> If nchars would be less than 128, the tests would be skipped.
>>>>>>>>>
>>>>>>>>> Should we add a check that nchars > 128 at the beginning and replace
>>>>>>>>> the
>>>>>>>>> "MAX (0, nchars - 128)" with only "nchars - 128"?
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> This is a separate issue beyond BZ #21982.
>>>>>>>>
>>>>>>>>
>>>>>>> Your patch is introducing this behaviour.
>>>>>>> Before your patch, nchars and outer was an int and the
>>>>>>> for-loop-condition
>>>>>>> "outer >= MAX (0, nchars - 128)" does not lead to an infinite loop or
>>>>>>> to
>>>>>>> skipping the test if nchars <= 128.
>>>>>>>
>>>>>>
>>>>>> How about this patch?
>>>>>>
>>>>> This solves the cases if nchars < 128.
>>>>> But if nchars == 128, then the condition of the for-loop is "size_t outer
>>>>> >=
>>>>> 0", which is always true.
>>>>>
>>>>> Could we check once if nchars > 128 and exit the test with an error if
>>>>> nchars is <= 128?
>>>>> Are there architectures where the page size is < 4096?
>>>>> Or where wchar_t > 4byte?
>>>>>
>>>>
>>>> Here is the updated patch.  I added
>>>>
>>>>   if (outer == 0)
>>>>     break;
>>>>
>>>> at the end of loop.
>>>>
>>>
>>> Okay. This fixes the case nchars == 128.
>>> I've retested this patch on s390x with gcc 7 -O3 and the warnings does not
>>> occur anymore.
>>
>> I am checking it in shortly.
>>
>
> I'd like to backport it to 2.25 and 2.26 branches.   Any comments?
>

I am checking it in now.


-- 
H.J.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] string/stratcliff.c: Replace int with size_t [BZ #21982]
       [not found]               ` <CAMe9rOrzLS5LnRWdrir_Uyn_UPicwNv+L-6S4Bc_Z=ErVbD=ew@mail.gmail.com>
@ 2017-01-01  0:00                 ` H.J. Lu
  2017-01-01  0:00                   ` H.J. Lu
  0 siblings, 1 reply; 2+ messages in thread
From: H.J. Lu @ 2017-01-01  0:00 UTC (permalink / raw)
  To: Stefan Liebler, Libc-stable Mailing List; +Cc: GNU C Library

On Wed, Aug 23, 2017 at 7:49 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Tue, Aug 22, 2017 at 7:56 AM, Stefan Liebler <stli@linux.vnet.ibm.com> wrote:
>> On 08/22/2017 02:43 PM, H.J. Lu wrote:
>>>
>>> On Tue, Aug 22, 2017 at 5:07 AM, Stefan Liebler <stli@linux.vnet.ibm.com>
>>> wrote:
>>>>
>>>> On 08/22/2017 01:05 AM, H.J. Lu wrote:
>>>>>
>>>>>
>>>>> On Mon, Aug 21, 2017 at 8:41 AM, Stefan Liebler
>>>>> <stli@linux.vnet.ibm.com>
>>>>> wrote:
>>>>>>
>>>>>>
>>>>>> On 08/21/2017 04:53 PM, H.J. Lu wrote:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Mon, Aug 21, 2017 at 6:48 AM, Stefan Liebler
>>>>>>> <stli@linux.vnet.ibm.com>
>>>>>>> wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> On 08/20/2017 07:17 PM, H.J. Lu wrote:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Fix GCC 7 errors when string/stratcliff.c is compiled with -O3:
>>>>>>>>>
>>>>>>>>> stratcliff.c: In function ‘do_test’:
>>>>>>>>> cc1: error: assuming signed overflow does not occur when assuming
>>>>>>>>> that
>>>>>>>>> (X
>>>>>>>>> - c) <= X is always true [-Werror=strict-overflow]
>>>>>>>>>
>>>>>>>>> OK for master?
>>>>>>>>>
>>>>>>>>> H.J.
>>>>>>>>> ---
>>>>>>>>>            [BZ #21982]
>>>>>>>>>            * string/stratcliff.c (do_test): Declare size, nchars,
>>>>>>>>> inner,
>>>>>>>>>            middle and outer with size_t instead of int.  Repleace %d
>>>>>>>>> with
>>>>>>>>>            %Zd in printf.
>>>>>>>>> ---
>>>>>>>>>      string/stratcliff.c | 72
>>>>>>>>> ++++++++++++++++++++++++++---------------------------
>>>>>>>>>      1 file changed, 36 insertions(+), 36 deletions(-)
>>>>>>>>>
>>>>>>>>> diff --git a/string/stratcliff.c b/string/stratcliff.c
>>>>>>>>> index e28b0c5058..ae780379cb 100644
>>>>>>>>> --- a/string/stratcliff.c
>>>>>>>>> +++ b/string/stratcliff.c
>>>>>>>>> @@ -58,8 +58,8 @@
>>>>>>>>>      int
>>>>>>>>>      do_test (void)
>>>>>>>>>      {
>>>>>>>>> -  int size = sysconf (_SC_PAGESIZE);
>>>>>>>>> -  int nchars = size / sizeof (CHAR);
>>>>>>>>> +  size_t size = sysconf (_SC_PAGESIZE);
>>>>>>>>> +  size_t nchars = size / sizeof (CHAR);
>>>>>>>>>        CHAR *adr;
>>>>>>>>>        CHAR *dest;
>>>>>>>>>        int result = 0;
>>>>>>>>> @@ -80,7 +80,7 @@ do_test (void)
>>>>>>>>>          }
>>>>>>>>>        else
>>>>>>>>>          {
>>>>>>>>> -      int inner, middle, outer;
>>>>>>>>> +      size_t inner, middle, outer;
>>>>>>>>>
>>>>>>>>>            mprotect (adr, size, PROT_NONE);
>>>>>>>>>            mprotect (adr + 2 * nchars, size, PROT_NONE);
>>>>>>>>> @@ -101,7 +101,7 @@ do_test (void)
>>>>>>>>>
>>>>>>>>>                  if (STRLEN (&adr[outer]) != (size_t) (inner -
>>>>>>>>> outer))
>>>>>>>>>                    {
>>>>>>>>> -                 printf ("%s flunked for outer = %d, inner = %d\n",
>>>>>>>>> +                 printf ("%s flunked for outer = %Zd, inner =
>>>>>>>>> %Zd\n",
>>>>>>>>>                              STRINGIFY (STRLEN), outer, inner);
>>>>>>>>>                      result = 1;
>>>>>>>>>                    }
>>>>>>>>>                    {
>>>>>>>>> -                 printf ("%s flunked for outer = %d, middle =
>>>>>>>>> %d\n",
>>>>>>>>> +                 printf ("%s flunked for outer = %Zd, middle =
>>>>>>>>> %Zd\n",
>>>>>>>>>                              STRINGIFY (rawmemchr), outer, middle);
>>>>>>>>>                      result = 1;
>>>>>>>>>                    }
>>>>>>>>> Hi H.J. Lu,
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> I've applied your patch and the warnings does not occur anymore on
>>>>>>>> s390.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Great.
>>>>>>>
>>>>>>>> The outer loops of the string tests are all using the following:
>>>>>>>> size_t nchars, outer;
>>>>>>>> for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
>>>>>>>>
>>>>>>>> I think we can assume, that nchars is always > 128 as it is derived
>>>>>>>> by
>>>>>>>> the
>>>>>>>> pagesize.
>>>>>>>> But if nchars would be equal to 128, this would result in an infinite
>>>>>>>> loop
>>>>>>>> (outer >= 0)?
>>>>>>>> If nchars would be less than 128, the tests would be skipped.
>>>>>>>>
>>>>>>>> Should we add a check that nchars > 128 at the beginning and replace
>>>>>>>> the
>>>>>>>> "MAX (0, nchars - 128)" with only "nchars - 128"?
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> This is a separate issue beyond BZ #21982.
>>>>>>>
>>>>>>>
>>>>>> Your patch is introducing this behaviour.
>>>>>> Before your patch, nchars and outer was an int and the
>>>>>> for-loop-condition
>>>>>> "outer >= MAX (0, nchars - 128)" does not lead to an infinite loop or
>>>>>> to
>>>>>> skipping the test if nchars <= 128.
>>>>>>
>>>>>
>>>>> How about this patch?
>>>>>
>>>> This solves the cases if nchars < 128.
>>>> But if nchars == 128, then the condition of the for-loop is "size_t outer
>>>> >=
>>>> 0", which is always true.
>>>>
>>>> Could we check once if nchars > 128 and exit the test with an error if
>>>> nchars is <= 128?
>>>> Are there architectures where the page size is < 4096?
>>>> Or where wchar_t > 4byte?
>>>>
>>>
>>> Here is the updated patch.  I added
>>>
>>>   if (outer == 0)
>>>     break;
>>>
>>> at the end of loop.
>>>
>>
>> Okay. This fixes the case nchars == 128.
>> I've retested this patch on s390x with gcc 7 -O3 and the warnings does not
>> occur anymore.
>
> I am checking it in shortly.
>

I'd like to backport it to 2.25 and 2.26 branches.   Any comments?

-- 
H.J.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-09-11 15:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20170820171713.GA19531@gmail.com>
     [not found] ` <25604b34-7afb-7007-4ea8-3add9963d4b4@linux.vnet.ibm.com>
     [not found]   ` <CAMe9rOq=oRRMrLg1=mJFcBszKUDGkPOgQ=dGqgQ9p9Vvqm=tkg@mail.gmail.com>
     [not found]     ` <b3944bf8-07a2-6460-a564-f973db7ea7fa@linux.vnet.ibm.com>
     [not found]       ` <CAMe9rOrBY3HVR1Quzcrg_Exj1VZCqX21wiwwqybeesToqZ2L9g@mail.gmail.com>
     [not found]         ` <10fd8eb0-0f53-9d4b-dcd2-694a1878c994@linux.vnet.ibm.com>
     [not found]           ` <CAMe9rOppYs6+kPif2++WrF3sAcphhWgw_bf04EVDp7r5fqO_Lg@mail.gmail.com>
     [not found]             ` <3cf66cda-dcdf-1e56-d0d9-1f015d486692@linux.vnet.ibm.com>
     [not found]               ` <CAMe9rOrzLS5LnRWdrir_Uyn_UPicwNv+L-6S4Bc_Z=ErVbD=ew@mail.gmail.com>
2017-01-01  0:00                 ` [PATCH] string/stratcliff.c: Replace int with size_t [BZ #21982] H.J. Lu
2017-01-01  0:00                   ` H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).