Minor GCC documentation correction for -Wformat-overflow

Message ID 099f32f6-4972-7617-e407-f506877b232b@oracle.com
State New
Headers show
Series
  • Minor GCC documentation correction for -Wformat-overflow
Related show

Commit Message

Indu Bhagat Feb. 14, 2018, 9:28 p.m.
In section "-Wformat-overflow=1", following is stated :

void f (int a, int b)
{
   char buf [12];
   sprintf (buf, "a = %i, b = %i\n", a, b);
}

" Increasing the size of the buffer by a single byte is sufficient to avoid
the warning,"

[size of an unknown int for the purpose of this warning is = 1 (to represent 0);
add 1 for newline, add 1 for null; add all the other chars in the format
string = 14]

The minimum increase however needs to be of 2 bytes. i.e., a buf of size 14 is
the minimum length for the warning in the example to go away.
So the correct statement should be -

" Increasing the size of the buffer by two bytes is sufficient to avoid the
warning,"

Alternatively, the size of buf can be bumped up to 13 in the sample code as done
in the patch below.

Thanks
  
--------------
gcc/ChangeLog:


	* doc/invoke.texi: Correction in -Wformat-overflow code sample.

Comments

Martin Sebor Feb. 15, 2018, 12:48 a.m. | #1
On 02/14/2018 02:28 PM, Indu Bhagat wrote:
> In section "-Wformat-overflow=1", following is stated :

>

> void f (int a, int b)

> {

>   char buf [12];

>   sprintf (buf, "a = %i, b = %i\n", a, b);

> }

>

> " Increasing the size of the buffer by a single byte is sufficient to avoid

> the warning,"

>

> [size of an unknown int for the purpose of this warning is = 1 (to

> represent 0);

> add 1 for newline, add 1 for null; add all the other chars in the format

> string = 14]

>

> The minimum increase however needs to be of 2 bytes. i.e., a buf of size

> 14 is

> the minimum length for the warning in the example to go away.

> So the correct statement should be -

>

> " Increasing the size of the buffer by two bytes is sufficient to avoid the

> warning,"

>

> Alternatively, the size of buf can be bumped up to 13 in the sample code

> as done

> in the patch below.


You're right, that's an off-by-one error/typo in the example.
Your patch seems like an obvious fix that can go in without
a formal approval, so I've committed it for you in r257680.

Thank you!
Martin

Patch

Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 257646)
+++ gcc/doc/invoke.texi	(working copy)
@@ -4184,7 +4184,7 @@ 
 @smallexample
 void f (int a, int b)
 @{
-  char buf [12];
+  char buf [13];
   sprintf (buf, "a = %i, b = %i\n", a, b);
 @}
 @end smallexample