public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds
@ 2004-01-28 13:23 rhajdaj at yahoo dot com
  2004-01-28 13:58 ` [Bug c/13896] " schwab at suse dot de
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: rhajdaj at yahoo dot com @ 2004-01-28 13:23 UTC (permalink / raw)
  To: gcc-bugs

If the following string array is declared,

char a[][3] = {{"Hello"}, {"Hi"}};

the following warning will be printed when the source file is compiled:
warning: initializer-string for array of chars is too long
warning: (near initialization for `a[0]')

If the array is changed to this,

char a[][3] = {{"Hel"}, {"Hi"}};

no warning is printed.  But of course a[1][0] ('H') overwrites the NULL-
terminator at a[0][3], so a[0] prints as "HelHi".  A warning should be printed 
for this.

-- 
           Summary: Initializer string warning not printed if implied NULL-
                    terminator out of bounds
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rhajdaj at yahoo dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
@ 2004-01-28 13:58 ` schwab at suse dot de
  2004-01-28 14:20 ` bangerth at dealii dot org
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: schwab at suse dot de @ 2004-01-28 13:58 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From schwab at suse dot de  2004-01-28 13:58 -------
Not a bug.  When a char array is initialized with a string literal the 
terminating NUL is not required to fit. 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
  2004-01-28 13:58 ` [Bug c/13896] " schwab at suse dot de
@ 2004-01-28 14:20 ` bangerth at dealii dot org
  2004-01-28 14:42 ` schwab at suse dot de
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: bangerth at dealii dot org @ 2004-01-28 14:20 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-01-28 14:20 -------
I tend to disagree. It may not be _required_ to fit, but I think that 
a warning is in order anyway. Andreas, where is this requirement 
stated? 
 
W. 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
  2004-01-28 13:58 ` [Bug c/13896] " schwab at suse dot de
  2004-01-28 14:20 ` bangerth at dealii dot org
@ 2004-01-28 14:42 ` schwab at suse dot de
  2004-01-28 14:47 ` bangerth at dealii dot org
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: schwab at suse dot de @ 2004-01-28 14:42 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From schwab at suse dot de  2004-01-28 14:42 -------
char a[][3] = {{"Hel"}, {"Hi"}}; 
 
is required to be equivalent to 
 
char a[][3] = {{'H', 'e', 'l'}, {'H', 'i', '\0'}}; 
 
See 6.7.8#14. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (2 preceding siblings ...)
  2004-01-28 14:42 ` schwab at suse dot de
@ 2004-01-28 14:47 ` bangerth at dealii dot org
  2004-01-28 15:26 ` schwab at suse dot de
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: bangerth at dealii dot org @ 2004-01-28 14:47 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-01-28 14:47 -------
OK, thanks. I still think this case merits a warning. Would you object 
to this? 
 
W. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (3 preceding siblings ...)
  2004-01-28 14:47 ` bangerth at dealii dot org
@ 2004-01-28 15:26 ` schwab at suse dot de
  2004-01-28 15:52 ` bangerth at dealii dot org
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: schwab at suse dot de @ 2004-01-28 15:26 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From schwab at suse dot de  2004-01-28 15:26 -------
I don't think there should be a warning by default, because it is a rather 
common idiom.  "initializer-string for array of chars is too long" would be 
wrong at least. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (4 preceding siblings ...)
  2004-01-28 15:26 ` schwab at suse dot de
@ 2004-01-28 15:52 ` bangerth at dealii dot org
  2004-01-28 16:37 ` pinskia at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: bangerth at dealii dot org @ 2004-01-28 15:52 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-01-28 15:52 -------
It doesn't have to be on by default, of course. 
 
I don't have strong opinions either way. If someone wants to contribute 
a third opinion, feel free to do whatever with this PR. 
 
W. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (5 preceding siblings ...)
  2004-01-28 15:52 ` bangerth at dealii dot org
@ 2004-01-28 16:37 ` pinskia at gcc dot gnu dot org
  2004-01-28 16:52 ` rhajdaj at yahoo dot com
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-01-28 16:37 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |diagnostic


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (6 preceding siblings ...)
  2004-01-28 16:37 ` pinskia at gcc dot gnu dot org
@ 2004-01-28 16:52 ` rhajdaj at yahoo dot com
  2004-01-28 16:56 ` pinskia at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: rhajdaj at yahoo dot com @ 2004-01-28 16:52 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From rhajdaj at yahoo dot com  2004-01-28 16:51 -------
I suppose it's blasphemy to disagree with that requirement, but since '\0' is 
indeed an element of any array containing it (implicitly or otherwise), it's my 
opinion that the requirement (though it might satisfy the letter of some law), 
disagrees with descriptions of string literals like (K & R p. 38):

"The internal representation of a string has a null character '\0' at the end, 
so the physical storage required is one more than the number of characters 
written between the quotes."

So a[0][] (using the a[][3] = {{"Hel"}, {"Hi"}} example) must take up 4 bytes.  
Given the requirement, you're right.  I got nothin.  But the requirement seems 
bogus to me.



-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (7 preceding siblings ...)
  2004-01-28 16:52 ` rhajdaj at yahoo dot com
@ 2004-01-28 16:56 ` pinskia at gcc dot gnu dot org
  2004-01-28 17:35 ` schwab at suse dot de
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-01-28 16:56 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-01-28 16:56 -------
K & R is old and should not be used any more for reading about C (likewise for ARM for C++).  
There is a standard C now and 6.7.8#14 of the standard says the oposite of K&R.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (8 preceding siblings ...)
  2004-01-28 16:56 ` pinskia at gcc dot gnu dot org
@ 2004-01-28 17:35 ` schwab at suse dot de
  2004-01-28 18:13 ` rhajdaj at yahoo dot com
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: schwab at suse dot de @ 2004-01-28 17:35 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From schwab at suse dot de  2004-01-28 17:34 -------
There is actually no contradiction here.  A string literal still contains the 
terminating NUL, but the initializer syntax is a special case that just happens 
to look like a string literal.  Even K&R C already had that. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (9 preceding siblings ...)
  2004-01-28 17:35 ` schwab at suse dot de
@ 2004-01-28 18:13 ` rhajdaj at yahoo dot com
  2004-01-29  1:17 ` pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: rhajdaj at yahoo dot com @ 2004-01-28 18:13 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From rhajdaj at yahoo dot com  2004-01-28 18:13 -------
You're right.  This compiler works in agreement with the requirement, so I 
should stop bugging you guys.  I guess I'm just not seeing the "why" behind the 
requirement.  When in is it a perfectly acceptable condition (ie according to -
Wall) not to null-terminate a literal string in an array like this?  (I don't 
understand the "if there's room" part of the requirement.)  And who do I talk 
to to challenge requirements so I can leave you alone?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (10 preceding siblings ...)
  2004-01-28 18:13 ` rhajdaj at yahoo dot com
@ 2004-01-29  1:17 ` pinskia at gcc dot gnu dot org
  2004-01-29 11:23 ` rhajdaj at yahoo dot com
  2004-05-20 20:23 ` trav at mac dot com
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-01-29  1:17 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-01-29 01:17 -------
I do not think a warning message is right here at all so closing the bug.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (11 preceding siblings ...)
  2004-01-29  1:17 ` pinskia at gcc dot gnu dot org
@ 2004-01-29 11:23 ` rhajdaj at yahoo dot com
  2004-05-20 20:23 ` trav at mac dot com
  13 siblings, 0 replies; 15+ messages in thread
From: rhajdaj at yahoo dot com @ 2004-01-29 11:23 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From rhajdaj at yahoo dot com  2004-01-29 11:23 -------
Hmm.  Sorry to have bothered you all.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

* [Bug c/13896] Initializer string warning not printed if implied NULL-terminator out of bounds
  2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
                   ` (12 preceding siblings ...)
  2004-01-29 11:23 ` rhajdaj at yahoo dot com
@ 2004-05-20 20:23 ` trav at mac dot com
  13 siblings, 0 replies; 15+ messages in thread
From: trav at mac dot com @ 2004-05-20 20:23 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From trav at mac dot com  2004-05-19 21:44 -------
(In reply to comment #10)
> You're right.  This compiler works in agreement with the requirement, so I 
> should stop bugging you guys.  I guess I'm just not seeing the "why" behind the 
> requirement.  When in is it a perfectly acceptable condition (ie according to -
> Wall) not to null-terminate a literal string in an array like this?  (I don't 
> understand the "if there's room" part of the requirement.)  And who do I talk 
> to to challenge requirements so I can leave you alone?
> 

Here is the rationale:

Since it is an array of characters, you should be able to use a convenient initializer to specify every 
element of the array, including the last. Thus, you are allowed to use a double-quoted string, and no 
null-character will be stored.

Not that I agree with making it so easy to create a unterminated string...


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13896


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

end of thread, other threads:[~2004-05-19 21:44 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-28 13:23 [Bug c/13896] New: Initializer string warning not printed if implied NULL-terminator out of bounds rhajdaj at yahoo dot com
2004-01-28 13:58 ` [Bug c/13896] " schwab at suse dot de
2004-01-28 14:20 ` bangerth at dealii dot org
2004-01-28 14:42 ` schwab at suse dot de
2004-01-28 14:47 ` bangerth at dealii dot org
2004-01-28 15:26 ` schwab at suse dot de
2004-01-28 15:52 ` bangerth at dealii dot org
2004-01-28 16:37 ` pinskia at gcc dot gnu dot org
2004-01-28 16:52 ` rhajdaj at yahoo dot com
2004-01-28 16:56 ` pinskia at gcc dot gnu dot org
2004-01-28 17:35 ` schwab at suse dot de
2004-01-28 18:13 ` rhajdaj at yahoo dot com
2004-01-29  1:17 ` pinskia at gcc dot gnu dot org
2004-01-29 11:23 ` rhajdaj at yahoo dot com
2004-05-20 20:23 ` trav at mac dot com

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 read-only IMAP folder(s) and NNTP newsgroup(s).