avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] [PATCH] printf: %s arguments with precision


From: Joerg Wunsch
Subject: Re: [avr-libc-dev] [PATCH] printf: %s arguments with precision
Date: Mon, 2 Feb 2004 11:32:22 +0100
User-agent: Mutt/1.2.5i

As Christopher Hoover wrote:

> printf and friends improperly handle non-null termnated strings when a
> %s argument has a precision.
> 
> "improperly handle" in this case is often a fatal error as the
> function is doing (effectively) strlen().  :-(

Thanks for pointing this out.  I wasn't aware that the standard doesn't
require the string to be NUL-terminated when a precision is present.

> -                                     for (base = 0; a.pc[base]; base++)
> -                                             ; /* calc length of string */
>  #if PRINTF_LEVEL > PRINTF_MIN
> -                                     if ((flags & FLPREC) && prec < base)
> -                                             base = prec;
> +                                     if (flags & FLPREC)
> +                                             base = strnlen(a.pc, prec);
> +                                     else
> +                                             base = strlen(a.pc);
>                                       width -= base;
> +#else
> +                                     base = strlen(a.pc);

I would prefer a version that doesn't call strlen()/strnlen() though, in
particular in the PRINTF_MIN case.
-- 
J"org Wunsch                                           Unix support engineer
address@hidden        http://www.interface-systems.de/~j/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]