linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* vsnprintf patch
@ 2003-08-13 11:52 Adrian Reber
  2003-08-13 12:16 ` Andreas Schwab
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Adrian Reber @ 2003-08-13 11:52 UTC (permalink / raw)
  To: linux-kernel


When using the snprintf function from the kernel the length returned is
not the length written:

len = snprintf(test,1,"BLA 1"); 

len is 5 although test is "B"

the patch below fixes the symptom, but I am not sure if this is the real
solution for this problem.


--- linux-2.6.0-test3.orig/lib/vsprintf.c       2003-08-09 06:40:52.000000000 +0200
+++ linux-2.6.0-test3/lib/vsprintf.c    2003-08-13 13:41:15.000000000 +0200
@@ -455,7 +455,10 @@
        /* the trailing null byte doesn't count towards the total
        * ++str;
        */
-       return str-buf;
+       if (str-buf > size)
+               return size;
+       else
+               return str-buf;
 }
 
 /**



		Adrian

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

* Re: vsnprintf patch
  2003-08-13 11:52 vsnprintf patch Adrian Reber
@ 2003-08-13 12:16 ` Andreas Schwab
  2003-08-13 12:19 ` viro
  2003-08-13 12:21 ` Bernd Petrovitsch
  2 siblings, 0 replies; 4+ messages in thread
From: Andreas Schwab @ 2003-08-13 12:16 UTC (permalink / raw)
  To: Adrian Reber; +Cc: linux-kernel

Adrian Reber <adrian@lisas.de> writes:

|> When using the snprintf function from the kernel the length returned is
|> not the length written:
|> 
|> len = snprintf(test,1,"BLA 1"); 
|> 
|> len is 5 although test is "B"

Exactly how it should be.

|> the patch below fixes the symptom, but I am not sure if this is the real
|> solution for this problem.

There is no problem here.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: vsnprintf patch
  2003-08-13 11:52 vsnprintf patch Adrian Reber
  2003-08-13 12:16 ` Andreas Schwab
@ 2003-08-13 12:19 ` viro
  2003-08-13 12:21 ` Bernd Petrovitsch
  2 siblings, 0 replies; 4+ messages in thread
From: viro @ 2003-08-13 12:19 UTC (permalink / raw)
  To: Adrian Reber; +Cc: linux-kernel

On Wed, Aug 13, 2003 at 01:52:12PM +0200, Adrian Reber wrote:
> 
> When using the snprintf function from the kernel the length returned is
> not the length written:
> 
> len = snprintf(test,1,"BLA 1"); 
> 
> len is 5 although test is "B"
> 
> the patch below fixes the symptom, but I am not sure if this is the real
> solution for this problem.

For what problem?  In the example above, 5 is correct return value.

7.19.6.5  The snprintf function

Synopsis

#include <stdio.h>

int snprintf(char * restrict s, size_t n, const char * restrict format, ...);

Description

The snprintf function is equivalent to fprintf, except that the output is
written into an array (specified by argument s) rather than to a stream.
If n is zero, nothing is written, and s may be a null pointer.  Otherwise,
output characters beyond the n-1st are discarded rather than being written
to the array, and a null character is written at the end of the characters
actually written into the array.  If copying takes place between objects
that overlap, the behavior is undefined.

Returns

The snprintf function returns the number of characters that would have
been written had n been sufficiently large, not counting the terminating
null character, or a negative value if an encoding error occurred.  Thus,
the null-terminated output has been completely written if and only if
the returned value is nonnegative and less than n.

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

* Re: vsnprintf patch
  2003-08-13 11:52 vsnprintf patch Adrian Reber
  2003-08-13 12:16 ` Andreas Schwab
  2003-08-13 12:19 ` viro
@ 2003-08-13 12:21 ` Bernd Petrovitsch
  2 siblings, 0 replies; 4+ messages in thread
From: Bernd Petrovitsch @ 2003-08-13 12:21 UTC (permalink / raw)
  To: Adrian Reber; +Cc: linux-kernel

On Mit, 2003-08-13 at 13:52, Adrian Reber wrote:
> When using the snprintf function from the kernel the length returned is
> not the length written:

This is how the function is defined:
http://www.opengroup.org/onlinepubs/007908799/xsh/fprintf.html

	Bernd
-- 
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services

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

end of thread, other threads:[~2003-08-13 12:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-08-13 11:52 vsnprintf patch Adrian Reber
2003-08-13 12:16 ` Andreas Schwab
2003-08-13 12:19 ` viro
2003-08-13 12:21 ` Bernd Petrovitsch

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