All of lore.kernel.org
 help / color / mirror / Atom feed
* "man 3 vsprintf" : simple bug in EXAMPLE section
@ 2015-03-27  5:27 Zhen Ren
       [not found] ` <551584DE020000D400069A4C-8qcz4lDFMZvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Zhen Ren @ 2015-03-27  5:27 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

Hi,
There is simple bug in EXAMPLE section of "man 3 vsprintf".

Function will return nothing if  it steps into the last "else {}".

------------------ example code -----------------
 char *
       make_message(const char *fmt, ...)
       {
           int n;
           int size = 100;     /* Guess we need no more than 100 bytes */
           char *p, *np;
           va_list ap;
.
cut off
.
               np = realloc(p, size);
               if (np == NULL) {
                   free(p);
                   return NULL;
               } else {
                   p = np;
+                return p;
               }
           }
       }
------------------------------------------------


--
Best regards,
Eric, Ren
HA team, SUSE


--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: "man 3 vsprintf" : simple bug in EXAMPLE section
       [not found] ` <551584DE020000D400069A4C-8qcz4lDFMZvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>
@ 2015-03-27  6:06   ` Michael Kerrisk (man-pages)
       [not found]     ` <CAKgNAkjY=YpuZ7ifA1HCN68jZZ_PgFRw4KYfKaXeViPAvs+m+w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Kerrisk (man-pages) @ 2015-03-27  6:06 UTC (permalink / raw)
  To: Zhen Ren; +Cc: linux-man

Hello Zhen Ren,

On 27 March 2015 at 06:27, Zhen Ren <zren-IBi9RG/b67k@public.gmane.org> wrote:
> Hi,
> There is simple bug in EXAMPLE section of "man 3 vsprintf".
>
> Function will return nothing if  it steps into the last "else {}".
>
> ------------------ example code -----------------
>  char *
>        make_message(const char *fmt, ...)
>        {
>            int n;
>            int size = 100;     /* Guess we need no more than 100 bytes */
>            char *p, *np;
>            va_list ap;
> .
> .cut off
> .
>                np = realloc(p, size);
>                if (np == NULL) {
>                    free(p);
>                    return NULL;
>                } else {
>                    p = np;
> +                return p;
>                }
>            }
>        }

I have not looked too closely at the code, but the above report
appears incorrect to me. The function should NOT be returning at that
point. It should be falling through and then round the loop once more.
If I've missed something, could you elaborate a little more on the
problem, please?

Cheers,

Michael
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: "man 3 vsprintf" : simple bug in EXAMPLE section
       [not found]     ` <CAKgNAkjY=YpuZ7ifA1HCN68jZZ_PgFRw4KYfKaXeViPAvs+m+w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2015-03-27  9:52       ` Zhen Ren
       [not found]         ` <5515C2F9020000D400069A6F-8qcz4lDFMZvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Zhen Ren @ 2015-03-27  9:52 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w; +Cc: linux-man

Hi Michael,

I'm so sorry for this negative report.

Yeah, you're right. It'll fall through,loop again,do "vsprintf" and return if done.

--
Best regards,
Eric, Ren
HA team, SUSE


>>> 
> Hello Zhen Ren, 
>  
> On 27 March 2015 at 06:27, Zhen Ren <zren-IBi9RG/b67k@public.gmane.org> wrote: 
> > Hi, 
> > There is simple bug in EXAMPLE section of "man 3 vsprintf". 
> > 
> > Function will return nothing if  it steps into the last "else {}". 
> > 
> > ------------------ example code ----------------- 
> >  char * 
> >        make_message(const char *fmt, ...) 
> >        { 
> >            int n; 
> >            int size = 100;     /* Guess we need no more than 100 bytes */ 
> >            char *p, *np; 
> >            va_list ap; 
> > . 
> > .cut off 
> > . 
> >                np = realloc(p, size); 
> >                if (np == NULL) { 
> >                    free(p); 
> >                    return NULL; 
> >                } else { 
> >                    p = np; 
> > +                return p; 
> >                } 
> >            } 
> >        } 
>  
> I have not looked too closely at the code, but the above report 
> appears incorrect to me. The function should NOT be returning at that 
> point. It should be falling through and then round the loop once more. 
> If I've missed something, could you elaborate a little more on the 
> problem, please? 
>  
> Cheers, 
>  
> Michael 
>  
>  


--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: "man 3 vsprintf" : simple bug in EXAMPLE section
       [not found]         ` <5515C2F9020000D400069A6F-8qcz4lDFMZvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>
@ 2015-03-27 10:20           ` walter harms
       [not found]             ` <55152F0A.7090708-fPG8STNUNVg@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: walter harms @ 2015-03-27 10:20 UTC (permalink / raw)
  To: Zhen Ren; +Cc: linux-man


hi Zhen Ren,
i tried to create a more simple example. Do you think this is more
helpful for you ?

re,
 wh

char *
make_message2 (const char *fmt, ...)
{
  int n,size = 0;
  char *p = NULL;
  va_list ap;

  /* figure our required size */
  va_start (ap, fmt);
  size = vsnprintf (p, size, fmt, ap);
  va_end (ap);

  if (size < 0)
    return NULL;

  /* leave room for \0 */
  size++;
  p = malloc (size);
  if (p == NULL)
    return NULL;

  va_start (ap, fmt);
  n = vsnprintf (p, size, fmt, ap);
  va_end (ap);

return p;
}


Am 27.03.2015 10:52, schrieb Zhen Ren:
> Hi Michael,
> 
> I'm so sorry for this negative report.
> 
> Yeah, you're right. It'll fall through,loop again,do "vsprintf" and return if done.
> 
> --
> Best regards,
> Eric, Ren
> HA team, SUSE
> 
> 
>>>>
>> Hello Zhen Ren, 
>>  
>> On 27 March 2015 at 06:27, Zhen Ren <zren-IBi9RG/b67k@public.gmane.org> wrote: 
>>> Hi, 
>>> There is simple bug in EXAMPLE section of "man 3 vsprintf". 
>>>
>>> Function will return nothing if  it steps into the last "else {}". 
>>>
>>> ------------------ example code ----------------- 
>>>  char * 
>>>        make_message(const char *fmt, ...) 
>>>        { 
>>>            int n; 
>>>            int size = 100;     /* Guess we need no more than 100 bytes */ 
>>>            char *p, *np; 
>>>            va_list ap; 
>>> . 
>>> .cut off 
>>> . 
>>>                np = realloc(p, size); 
>>>                if (np == NULL) { 
>>>                    free(p); 
>>>                    return NULL; 
>>>                } else { 
>>>                    p = np; 
>>> +                return p; 
>>>                } 
>>>            } 
>>>        } 
>>  
>> I have not looked too closely at the code, but the above report 
>> appears incorrect to me. The function should NOT be returning at that 
>> point. It should be falling through and then round the loop once more. 
>> If I've missed something, could you elaborate a little more on the 
>> problem, please? 
>>  
>> Cheers, 
>>  
>> Michael 
>>  
>>  
> 
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-man" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: "man 3 vsprintf" : simple bug in EXAMPLE section
       [not found]             ` <55152F0A.7090708-fPG8STNUNVg@public.gmane.org>
@ 2015-03-27 15:26               ` Zhen Ren
  2015-04-11  7:30               ` Michael Kerrisk (man-pages)
  1 sibling, 0 replies; 6+ messages in thread
From: Zhen Ren @ 2015-03-27 15:26 UTC (permalink / raw)
  To: wharms-fPG8STNUNVg; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

Hi walter,
The way you implemented is more straightforward and  more efficient.
No more realloc call which is expensive.

Thanks very much ;-)


--
Best regards,
 
Eric Ren
HA team,SUSE.


>>> walter harms <wharms-fPG8STNUNVg@public.gmane.org> 03/27/15 6:21 PM >>>

hi Zhen Ren,
i tried to create a more simple example. Do you think this is more
helpful for you ?

re,
 wh

char *
make_message2 (const char *fmt, ...)
{
  int n,size = 0;
  char *p = NULL;
  va_list ap;

  /* figure our required size */
  va_start (ap, fmt);
  size = vsnprintf (p, size, fmt, ap);
  va_end (ap);

  if (size < 0)
    return NULL;

  /* leave room for \0 */
  size++;
  p = malloc (size);
  if (p == NULL)
    return NULL;

  va_start (ap, fmt);
  n = vsnprintf (p, size, fmt, ap);
  va_end (ap);

return p;
}


Am 27.03.2015 10:52, schrieb Zhen Ren:
> Hi Michael,
> 
> I'm so sorry for this negative report.
> 
> Yeah, you're right. It'll fall through,loop again,do "vsprintf" and return if done.
> 
> --
> Best regards,
> Eric, Ren
> HA team, SUSE
> 
> 
>>>>
>> Hello Zhen Ren, 
>>  
>> On 27 March 2015 at 06:27, Zhen Ren <zren-IBi9RG/b67k@public.gmane.org> wrote: 
>>> Hi, 
>>> There is simple bug in EXAMPLE section of "man 3 vsprintf". 
>>>
>>> Function will return nothing if  it steps into the last "else {}". 
>>>
>>> ------------------ example code ----------------- 
>>>  char * 
>>>        make_message(const char *fmt, ...) 
>>>        { 
>>>            int n; 
>>>            int size = 100;     /* Guess we need no more than 100 bytes */ 
>>>            char *p, *np; 
>>>            va_list ap; 
>>> . 
>>> .cut off 
>>> . 
>>>                np = realloc(p, size); 
>>>                if (np == NULL) { 
>>>                    free(p); 
>>>                    return NULL; 
>>>                } else { 
>>>                    p = np; 
>>> +                return p; 
>>>                } 
>>>            } 
>>>        } 
>>  
>> I have not looked too closely at the code, but the above report 
>> appears incorrect to me. The function should NOT be returning at that 
>> point. It should be falling through and then round the loop once more. 
>> If I've missed something, could you elaborate a little more on the 
>> problem, please? 
>>  
>> Cheers, 
>>  
>> Michael 
>>  
>>  
> 
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-man" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html



--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: "man 3 vsprintf" : simple bug in EXAMPLE section
       [not found]             ` <55152F0A.7090708-fPG8STNUNVg@public.gmane.org>
  2015-03-27 15:26               ` Zhen Ren
@ 2015-04-11  7:30               ` Michael Kerrisk (man-pages)
  1 sibling, 0 replies; 6+ messages in thread
From: Michael Kerrisk (man-pages) @ 2015-04-11  7:30 UTC (permalink / raw)
  To: wharms-fPG8STNUNVg, Zhen Ren
  Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w, linux-man

On 03/27/2015 11:20 AM, walter harms wrote:
> 
> hi Zhen Ren,
> i tried to create a more simple example. Do you think this is more
> helpful for you ?

Walter,

Your version seems better. See comments below.

> re,
>  wh
> 
> char *
> make_message2 (const char *fmt, ...)
> {
>   int n,size = 0;
>   char *p = NULL;
>   va_list ap;
> 
>   /* figure our required size */

>   va_start (ap, fmt);
>   size = vsnprintf (p, size, fmt, ap);
>   va_end (ap);
> 
>   if (size < 0)
>     return NULL;
> 
>   /* leave room for \0 */
>   size++;
>   p = malloc (size);
>   if (p == NULL)
>     return NULL;
> 
>   va_start (ap, fmt);
>   n = vsnprintf (p, size, fmt, ap);

What's missing here is a check on the return value from
the vsnprintf() call. I added one.

I've put your code into the page, replacing the existing
example, with the fix noted above.

Thanks,

Michael



>   va_end (ap);
> 
> return p;
> }

> Am 27.03.2015 10:52, schrieb Zhen Ren:
>> Hi Michael,
>>
>> I'm so sorry for this negative report.
>>
>> Yeah, you're right. It'll fall through,loop again,do "vsprintf" and return if done.
>>
>> --
>> Best regards,
>> Eric, Ren
>> HA team, SUSE
>>
>>
>>>>>
>>> Hello Zhen Ren, 
>>>  
>>> On 27 March 2015 at 06:27, Zhen Ren <zren-IBi9RG/b67k@public.gmane.org> wrote: 
>>>> Hi, 
>>>> There is simple bug in EXAMPLE section of "man 3 vsprintf". 
>>>>
>>>> Function will return nothing if  it steps into the last "else {}". 
>>>>
>>>> ------------------ example code ----------------- 
>>>>  char * 
>>>>        make_message(const char *fmt, ...) 
>>>>        { 
>>>>            int n; 
>>>>            int size = 100;     /* Guess we need no more than 100 bytes */ 
>>>>            char *p, *np; 
>>>>            va_list ap; 
>>>> . 
>>>> .cut off 
>>>> . 
>>>>                np = realloc(p, size); 
>>>>                if (np == NULL) { 
>>>>                    free(p); 
>>>>                    return NULL; 
>>>>                } else { 
>>>>                    p = np; 
>>>> +                return p; 
>>>>                } 
>>>>            } 
>>>>        } 
>>>  
>>> I have not looked too closely at the code, but the above report 
>>> appears incorrect to me. The function should NOT be returning at that 
>>> point. It should be falling through and then round the loop once more. 
>>> If I've missed something, could you elaborate a little more on the 
>>> problem, please? 
>>>  
>>> Cheers, 
>>>  
>>> Michael 
>>>  
>>>  
>>
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-man" in
>> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> --
> To unsubscribe from this list: send the line "unsubscribe linux-man" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

end of thread, other threads:[~2015-04-11  7:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-27  5:27 "man 3 vsprintf" : simple bug in EXAMPLE section Zhen Ren
     [not found] ` <551584DE020000D400069A4C-8qcz4lDFMZvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>
2015-03-27  6:06   ` Michael Kerrisk (man-pages)
     [not found]     ` <CAKgNAkjY=YpuZ7ifA1HCN68jZZ_PgFRw4KYfKaXeViPAvs+m+w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-03-27  9:52       ` Zhen Ren
     [not found]         ` <5515C2F9020000D400069A6F-8qcz4lDFMZvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>
2015-03-27 10:20           ` walter harms
     [not found]             ` <55152F0A.7090708-fPG8STNUNVg@public.gmane.org>
2015-03-27 15:26               ` Zhen Ren
2015-04-11  7:30               ` Michael Kerrisk (man-pages)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.