zsh-workers
 help / color / mirror / code / Atom feed
* printf bug(s?)
@ 2005-06-17  0:58 Harald van Dijk
  2005-06-17  8:15 ` Oliver Kiddle
  0 siblings, 1 reply; 6+ messages in thread
From: Harald van Dijk @ 2005-06-17  0:58 UTC (permalink / raw)
  To: zsh-workers

Hi,

I'll get right to the point.

1:

When I run the command printf '%', zsh 4.2.5 gives me a segfault. It
should complain that % by itself is invalid instead. I digged around a
bit and it seems to be caused by zsh mistakenly treating '\0' as a valid
flag character. This part is easy enough to work around:

--- zsh-4.2.5.orig/Src/builtin.c
+++ zsh-4.2.5/Src/builtin.c
@@ -3651,7 +3651,7 @@

            /* copy only one of each flag as spec has finite size */
            memset(flags, 0, sizeof(flags));
-           while ((flag = strchr(flagch, *c))) {
+           while (*c!='\0' && (flag = strchr(flagch, *c))) {
                if (!flags[flag - flagch]) {
                    flags[flag - flagch] = 1;
                    *d++ = *c;

If *c=='\0', strchr doesn't return NULL, but instead, it returns a
pointer to the terminating '\0', so this case should get special treatment.

With this patch, zsh now prints
printf: %: invalid directive
which I consider more sane :-)

But perhaps there's a nicer way to scan for flag characters.

2:

That segfault I noticed when I tried to run printf '\045'. I expected
this to print a single character '%', the same as printf '%%' would. zsh
instead treats it exactly as an ordinary % character. Is this a bug, or
am I wrong to expect it to print '%'? I checked the zsh documentation,
but I couldn't find the answer there. I then checked
http://www.opengroup.org/onlinepubs/009695399/utilities/printf.html
and found
" "\ddd", where ddd is a one, two, or three-digit octal number, shall
  be written as a byte with the numeric value specified by the octal
  number. "
To me, this seems to mean that it should print just a '%', instead of
converting it to '%' and rereading it, though I may well be misreading
this. (It works as I expected with coreutils's printf, by the way.)

A patch for this is a bit too much for me at the moment though, even
moreso since I'd hate to work on it when it may turn out not to be a
bug, sorry.

Thanks in advance.


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

* Re: printf bug(s?)
  2005-06-17  0:58 printf bug(s?) Harald van Dijk
@ 2005-06-17  8:15 ` Oliver Kiddle
  2005-06-17 13:22   ` Harald van Dijk
  0 siblings, 1 reply; 6+ messages in thread
From: Oliver Kiddle @ 2005-06-17  8:15 UTC (permalink / raw)
  To: Harald van Dijk; +Cc: zsh-workers

Harald van D?k wrote:
> When I run the command printf '%', zsh 4.2.5 gives me a segfault. It

I can't reproduce the segfault on any system I have access to so perhaps
it is platform specific. Your fix looks right so I'll commit it to
CVS. Thanks.

> 2:
> 
> That segfault I noticed when I tried to run printf '\045'. I expected
> this to print a single character '%', the same as printf '%%' would. zsh
> instead treats it exactly as an ordinary % character. Is this a bug, or

It's not a bug as such. Note that it is entirely consistent with how
printf works from C. I think there was a brief discussion about it when
printf was first added.

The current implementation makes it easier to share the \ handling code
with echo. It just calls getkeystring() on the whole format
specification first. I seem to remember that, at the time, a survey of
how other printf implementations worked showed a complete mix so it
seemed justifiable to take the lazy option. However, I would agree that
your expected behaviour would probably be better.

> " "\ddd", where ddd is a one, two, or three-digit octal number, shall
>   be written as a byte with the numeric value specified by the octal
>   number. "

It's difficult to know whether it was intended by the wording or not.

However, that wording also means that \7 should output a bell character
(you need \07 at the moment). Annoyingly, the specification for how \
escapes work is subtly different for echo and printf. I think \c has a
different definition too.

So what we probably need to do is have the printf code handle \ escapes
itself at the same time as it scans through the specification looking
for format specifiers.

Oliver


This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.


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

* Re: printf bug(s?)
  2005-06-17  8:15 ` Oliver Kiddle
@ 2005-06-17 13:22   ` Harald van Dijk
  2005-06-17 13:48     ` J
  0 siblings, 1 reply; 6+ messages in thread
From: Harald van Dijk @ 2005-06-17 13:22 UTC (permalink / raw)
  To: zsh-workers

Oliver Kiddle wrote:
> Harald van D?k wrote:
> 
>>When I run the command printf '%', zsh 4.2.5 gives me a segfault. It
> 
> 
> I can't reproduce the segfault on any system I have access to so perhaps
> it is platform specific. Your fix looks right so I'll commit it to
> CVS. Thanks.
> 
I`m using a rather plain Linux system (glibc 2,3,5, gcc 3.4.4), and in
the end zsh was configured with --prefix=$HOME/zsh --enable-zsh-debug,
nothing special. (I'm not sure on the original configuration.) It
doesn't always segfault since it depends on whatever happens to be in
memory after the format string. A pretty reliable way for me to get it
segfaulted is with for x in %; do printf $(echo $x); done on the command
line, or much more simply, zsh -c 'printf %'. However, even just
entering 'printf %' on the command line segfaulted for me sometimes.

Though it doesn't really matter; I guess it's good if you can't
reproduce it :) Thanks.

>>2:
>>
>>That segfault I noticed when I tried to run printf '\045'. I expected
>>this to print a single character '%', the same as printf '%%' would. zsh
>>instead treats it exactly as an ordinary % character. Is this a bug, or
> 
> 
> It's not a bug as such. Note that it is entirely consistent with how
> printf works from C. I think there was a brief discussion about it when
> printf was first added.
> 
> The current implementation makes it easier to share the \ handling code
> with echo. It just calls getkeystring() on the whole format
> specification first. I seem to remember that, at the time, a survey of
> how other printf implementations worked showed a complete mix so it
> seemed justifiable to take the lazy option. However, I would agree that
> your expected behaviour would probably be better.
> 
Oh, there are other printfs that behave the same? That I didn't know; in
that case, I may be better off avoiding using \045 anyway and just
replace it with %%...

>>" "\ddd", where ddd is a one, two, or three-digit octal number, shall
>>  be written as a byte with the numeric value specified by the octal
>>  number. "
> 
> 
> It's difficult to know whether it was intended by the wording or not.
> 
> However, that wording also means that \7 should output a bell character
> (you need \07 at the moment). Annoyingly, the specification for how \
> escapes work is subtly different for echo and printf. I think \c has a
> different definition too.
> 
Ah, right. As another example, \0007 should print a single character
with echo, but two ('\0' and '7') with printf '\0007'. However, printf
'%b' '\0007' should still print a single character...

> So what we probably need to do is have the printf code handle \ escapes
> itself at the same time as it scans through the specification looking
> for format specifiers.
> 
Either that, or you could modify getkeystring to be able to treat the
string the way a printf format string should be treated. That seems to
be a bit less work to me. Though, putting it with the % scanning would
mean being able to avoid %\nnn... printf '%\0163\n' 'Hello' works right
now, and modifying getkeystring would make printf '%\163\n' 'Hello' work.

But I'll be happy with whatever you decide to do.

> Oliver
> 
> 
> This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.
> 


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

* Re: printf bug(s?)
  2005-06-17 13:22   ` Harald van Dijk
@ 2005-06-17 13:48     ` J
  2005-06-17 15:47       ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: J @ 2005-06-17 13:48 UTC (permalink / raw)
  To: Harald van Dijk; +Cc: zsh-workers

> Oh, there are other printfs that behave the same? That I didn't know; in
> that case, I may be better off avoiding using \045 anyway and just
> replace it with %%...

My understanding is that for all intents and purposes, printf should
consider a character designed by his octal code as the character
itself, be it a control character or not, pretty much the same way
that the shell interprets it before handing it to the command when
it's enclosed in $'...'. If I run
$ printf '\045i' '5+1'
I would certainly expect it to print 6, and not %i.

Of course that's just what I would expect as a user. Other people may disagree.

-- 
J


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

* Re: printf bug(s?)
  2005-06-17 13:48     ` J
@ 2005-06-17 15:47       ` Bart Schaefer
  2005-06-21  9:19         ` Oliver Kiddle
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2005-06-17 15:47 UTC (permalink / raw)
  To: zsh-workers

On Jun 17,  3:48pm, J wrote:
} Subject: Re: printf bug(s?)
}
} > I may be better off avoiding using \045 anyway and just
} > replace it with %%...
} 
} If I run
} $ printf '\045i' '5+1'
} I would certainly expect it to print 6, and not %i.

Is the printf builtin (or shell utility) specified by POSIX?  If so,
this may be a reasonable question for austin-group interpretation.


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

* Re: printf bug(s?)
  2005-06-17 15:47       ` Bart Schaefer
@ 2005-06-21  9:19         ` Oliver Kiddle
  0 siblings, 0 replies; 6+ messages in thread
From: Oliver Kiddle @ 2005-06-21  9:19 UTC (permalink / raw)
  To: zsh-workers

On 17 Jun, Bart wrote:
> Is the printf builtin (or shell utility) specified by POSIX?  If so,
> this may be a reasonable question for austin-group interpretation.

It is specified by POSIX:
  http://www.opengroup.org/onlinepubs/009695399/utilities/printf.html

I don't subscribe to the austin-group lists so if you do get any
interpretation there, let us know the results.

Oliver


This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.


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

end of thread, other threads:[~2005-06-21  9:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-17  0:58 printf bug(s?) Harald van Dijk
2005-06-17  8:15 ` Oliver Kiddle
2005-06-17 13:22   ` Harald van Dijk
2005-06-17 13:48     ` J
2005-06-17 15:47       ` Bart Schaefer
2005-06-21  9:19         ` Oliver Kiddle

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).