linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* %n in fscanf
@ 2010-03-08 11:38 Joern Heissler
       [not found] ` <4B94E1C8.8010100-pbvhy9/vjlfAiHgwPnP32rNAH6kLmebB@public.gmane.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Joern Heissler @ 2010-03-08 11:38 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

The fscanf manpage contains this:

n      Nothing  is  expected;  instead, the number of characters consumed thus far from the input is 
stored through the next pointer, which must be a pointer to int.  This is not a conversion, although it 
can be suppressed with the * assignment-suppression character.  The  C  standard  says:
"Execution  of  a  %n  directive does not increment the assignment count returned at the completion of 
execution" but the Corrigendum seems to contradict this.  Probably it is wise not to make any 
assumptions on the effect of %n conversions on the return value.


I wondered about this and looked at c89, c99+tc3, c1x, k&r2, posix manpages; all say that %n does *not* 
increase the counter. %*n causes undefined behaviour according to c99+tc3.
I wasn't able to find proof that any Corrigendum says otherwise.
Therefore I think it's safe to say that you can indeed make the assumption that %n does not affect the 
return value.

Suggested patch:
--- scanf.3.orig        2010-03-08 12:21:59.000000000 +0100
+++ scanf.3     2010-03-08 12:29:59.000000000 +0100
@@ -506,18 +506,15 @@
  .IR int .
  This is
  .I not
-a conversion, although it can be suppressed with the
-.B *
-assignment-suppression character.
-The C standard says: "Execution of a
-.B %n
-directive does not increment
-the assignment count returned at the completion of execution"
-but the Corrigendum seems to contradict this.
-Probably it is wise
-not to make any assumptions on the effect of
-.B %n
-conversions on the return value.
+a conversion and does
+.I not
+increase the count returned by the function.
+The assignment can be suppressed with the
+.B *
+assignment-suppression character, but the effect on the
+return value is undefined. Therefore
+.B %*n
+conversions should not be used.
  .SH "RETURN VALUE"
  These functions return the number of input items
  successfully matched and assigned,


Cheers,
Jörn Heissler
--
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] 2+ messages in thread

* Re: %n in fscanf
       [not found] ` <4B94E1C8.8010100-pbvhy9/vjlfAiHgwPnP32rNAH6kLmebB@public.gmane.org>
@ 2015-05-04 13:13   ` Michael Kerrisk (man-pages)
  0 siblings, 0 replies; 2+ messages in thread
From: Michael Kerrisk (man-pages) @ 2015-05-04 13:13 UTC (permalink / raw)
  To: Joern Heissler
  Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w,
	linux-man-u79uwXL29TY76Z2rM5mHXA

Joern,

On 03/08/2010 12:38 PM, Joern Heissler wrote:
> The fscanf manpage contains this:
> 
> n      Nothing  is  expected;  instead, the number of characters consumed thus far from the input is 
> stored through the next pointer, which must be a pointer to int.  This is not a conversion, although it 
> can be suppressed with the * assignment-suppression character.  The  C  standard  says:
> "Execution  of  a  %n  directive does not increment the assignment count returned at the completion of 
> execution" but the Corrigendum seems to contradict this.  Probably it is wise not to make any 
> assumptions on the effect of %n conversions on the return value.
> 
> 
> I wondered about this and looked at c89, c99+tc3, c1x, k&r2, posix manpages; all say that %n does *not* 
> increase the counter. %*n causes undefined behaviour according to c99+tc3.
> I wasn't able to find proof that any Corrigendum says otherwise.
> Therefore I think it's safe to say that you can indeed make the assumption that %n does not affect the 
> return value.

Long after the fact, I've applied this patch.

Cheers,

Michael



> Suggested patch:
> --- scanf.3.orig        2010-03-08 12:21:59.000000000 +0100
> +++ scanf.3     2010-03-08 12:29:59.000000000 +0100
> @@ -506,18 +506,15 @@
>   .IR int .
>   This is
>   .I not
> -a conversion, although it can be suppressed with the
> -.B *
> -assignment-suppression character.
> -The C standard says: "Execution of a
> -.B %n
> -directive does not increment
> -the assignment count returned at the completion of execution"
> -but the Corrigendum seems to contradict this.
> -Probably it is wise
> -not to make any assumptions on the effect of
> -.B %n
> -conversions on the return value.
> +a conversion and does
> +.I not
> +increase the count returned by the function.
> +The assignment can be suppressed with the
> +.B *
> +assignment-suppression character, but the effect on the
> +return value is undefined. Therefore
> +.B %*n
> +conversions should not be used.
>   .SH "RETURN VALUE"
>   These functions return the number of input items
>   successfully matched and assigned,
> 
> 
> Cheers,
> Jörn Heissler
> 


-- 
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] 2+ messages in thread

end of thread, other threads:[~2015-05-04 13:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-08 11:38 %n in fscanf Joern Heissler
     [not found] ` <4B94E1C8.8010100-pbvhy9/vjlfAiHgwPnP32rNAH6kLmebB@public.gmane.org>
2015-05-04 13:13   ` Michael Kerrisk (man-pages)

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).