Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   warning: string length greater than '509' (http://www.velocityreviews.com/forums/t807296-warning-string-length-greater-than-509-a.html)

JohnF 12-23-2011 10:01 AM

warning: string length greater than '509'
 
I like to compile (linux/gcc) with -pedantic -Wall
and neaten up the code to eliminate all warnings.
But the following code emits the message
warning: string length '1008' is greater than the
length '509' ISO C90 compilers are required to support
and I can't see a neat way around it. The offending
statement is,
static char latexwrapper[16384] =
"\\documentclass[10pt]{article}\n"
"\\usepackage[latin1]{inputenc}\n"
"\\usepackage{amsmath}\n"
"\\usepackage{amsfonts}\n"
"...lots-more-junk...";
I could obviously break it up into several smaller
initializations and strcat() them all at runtime,
but that (in my opinion) isn't what I meant by "neat".
It works as is, which is how I'd leave it if that's
(strcat() several smaller strings at runtime) is the
only solution. But a straightforward way around the
warning would be even nicer.
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )

jacob navia 12-23-2011 10:19 AM

Re: warning: string length greater than '509'
 
Le 23/12/11 11:01, JohnF a écrit :
> I like to compile (linux/gcc) with -pedantic -Wall
> and neaten up the code to eliminate all warnings.
> But the following code emits the message
> warning: string length '1008' is greater than the
> length '509' ISO C90 compilers are required to support
> and I can't see a neat way around it. The offending
> statement is,
> static char latexwrapper[16384] =
> "\\documentclass[10pt]{article}\n"
> "\\usepackage[latin1]{inputenc}\n"
> "\\usepackage{amsmath}\n"
> "\\usepackage{amsfonts}\n"
> "...lots-more-junk...";
> I could obviously break it up into several smaller
> initializations and strcat() them all at runtime,
> but that (in my opinion) isn't what I meant by "neat".
> It works as is, which is how I'd leave it if that's
> (strcat() several smaller strings at runtime) is the
> only solution. But a straightforward way around the
> warning would be even nicer.


You are using gcc in an obsolete mode. The minimum
string length any compiler is REQUIRED to support is
4095 characters as specified in the C99 standard
(5.2.4.1)

The compiler is warning that you have exceeded the
limit of specified in the 1989 version of the standard.

Anyway, you have exceeded the C99 limit too, so the compiler
is right to "complain" in PEDANTIC mode.

JohnF 12-23-2011 12:41 PM

Re: warning: string length greater than '509'
 
Robert Wessel <robertwessel2@yahoo.com> wrote:
> JohnF <john@please.see.sig.for.email.com> wrote:
>
>> warning: string length '1008' is greater than the
>> length '509' ISO C90 compilers are required to support

>
> Compile with: -Wno-overlength-strings


Who knew??? So I took a look at man cc and also found
-std=gnu99 which also works. Weirdly, though, -std=c99,
which seems equivalent as described on the manpage,
emits about a dozen new warnings, including some
"obviously wrong" (meaning they could be right?) ones
like "implicit declaration of function 'popen'", even
though stdio.h is included. (Note: I haven't actually
tested the executable images created using either switch,
and certainly not looked for any differences in the
generated instructions.)
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )

jacob navia 12-23-2011 12:52 PM

Re: warning: string length greater than '509'
 
Le 23/12/11 11:49, pete a écrit :

> The number (509) appears more than once
> in the C90 standard
> in a related context.
>
> ISO/IEC 9899: 1990


That is not the C standard, it has been obsoleted by the standard of
1999 where the enviromental limits have been increased to more
reasonable values.

JohnF 12-23-2011 01:04 PM

Re: warning: string length greater than '509'
 
pete <pfiland@mindspring.com> wrote:
> JohnF wrote:
>>
>> warning: string length '1008' is greater than the
>> length '509'
>> static char latexwrapper[16384] =
>> "\\documentclass[10pt]{article}\n"
>> "\\usepackage[latin1]{inputenc}\n"
>> "\\usepackage{amsmath}\n"
>> "\\usepackage{amsfonts}\n"
>> "...lots-more-junk...";

>
> if latexwrapper is supposed to be constant,


Nope, it contains embedded keywords like %%expression%%
(always something delimited by %%...%%) that later gets
substituted with the user's input expression, and various
other substitutions like font size and similar
gobbledy-gook. (And, yeah, I make sure the 16384 allocation
doesn't overflow.)

> static char latexwrapper[33][510] = /* {"",""} */


One big long contiguous string is what's really required
to neatly solve the problem. An array of shorter ones
represents the situation in a way that requires more
lines of code in several different places. (But I appreciate
how you calculated [33] to match my 16384 total:)

> The number (509) appears more than once in the C90 standard

Yes, it's a very nice number, indeed.
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )

Jens Thoms Toerring 12-23-2011 01:50 PM

Re: warning: string length greater than '509'
 
JohnF <john@please.see.sig.for.email.com> wrote:
> Robert Wessel <robertwessel2@yahoo.com> wrote:
> > JohnF <john@please.see.sig.for.email.com> wrote:
> >
> >> warning: string length '1008' is greater than the
> >> length '509' ISO C90 compilers are required to support

> >
> > Compile with: -Wno-overlength-strings


> Who knew??? So I took a look at man cc and also found
> -std=gnu99 which also works. Weirdly, though, -std=c99,
> which seems equivalent as described on the manpage,
> emits about a dozen new warnings, including some
> "obviously wrong" (meaning they could be right?) ones
> like "implicit declaration of function 'popen'", even
> though stdio.h is included.


popen() isn't a standard C function but is from POSIX. Thus
when compiling for absolutely clean C it won't be defined.
You will need to define some extra macro (e.g. define
_POSIX_C_SOURCE to at least 2 or define one of _XOPEN_SOURCE,
_BSD_SOURCE or _SVID_SOURCE before including <stdio.h>) that
results in popen() (and other non-standard C functions) to
become declared in <stdio.h> (but, of course, at a loss of
portability). What I'm surprised about though is that you
didn't get those complaints already when compiling for C89
- I get them with '-ansi -pedantic'.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

James Kuyper 12-23-2011 02:14 PM

Re: warning: string length greater than '509'
 
On 12/23/2011 07:41 AM, JohnF wrote:
> Robert Wessel <robertwessel2@yahoo.com> wrote:
>> JohnF <john@please.see.sig.for.email.com> wrote:
>>
>>> warning: string length '1008' is greater than the
>>> length '509' ISO C90 compilers are required to support

>>
>> Compile with: -Wno-overlength-strings

>
> Who knew??? So I took a look at man cc and also found
> -std=gnu99 which also works. Weirdly, though, -std=c99,
> which seems equivalent as described on the manpage,
> emits about a dozen new warnings, including some
> "obviously wrong" (meaning they could be right?) ones
> like "implicit declaration of function 'popen'", even
> though stdio.h is included.


Using -std=c99 imposes strict C99 mode. turning off a lot os OS-specific
extensions to the C standard library. popen() is one of those
extensions. There's several different ways to turn it on. You can define
_BSD_SOURCE, or _SVID_SOURCE, or _POSIX_C_SOURCE with a value >= 2, or
_XOPEN_SOURCE with a value >= 500, or simply define _GNU_SOURCE, which
causes all of the others to be set as well.
--
James Kuyper

Eric Sosman 12-23-2011 02:36 PM

Re: warning: string length greater than '509'
 
On 12/23/2011 8:04 AM, JohnF wrote:
> pete<pfiland@mindspring.com> wrote:
>> JohnF wrote:
>>>
>>> warning: string length '1008' is greater than the
>>> length '509'
>>> static char latexwrapper[16384] =
>>> "\\documentclass[10pt]{article}\n"
>>> "\\usepackage[latin1]{inputenc}\n"
>>> "\\usepackage{amsmath}\n"
>>> "\\usepackage{amsfonts}\n"
>>> "...lots-more-junk...";

>>
>> if latexwrapper is supposed to be constant,

>
> Nope, it contains embedded keywords like %%expression%%
> (always something delimited by %%...%%) that later gets
> substituted with the user's input expression, and various
> other substitutions like font size and similar
> gobbledy-gook. (And, yeah, I make sure the 16384 allocation
> doesn't overflow.)


Replacement in situ? Yukk! Not only does it pose a significant
risk of buffer overflow, but it also means that a string of this kind
is not reusable: Process it once, and all those %%foo%% bits have
vanished. And just to heighten the sense of outrage, the substitution
probably takes quadratic time ...

If you're still interested in staying within the 509-character
limit (thus ensuring that resource-constrained compilers won't choke
on your source code), I'd suggest chopping the big blob of text into
chunks of convenient size. Before processing, you can allocate a big
buffer and strcpy/strcat the little strings into it, then use the big
buffer exactly as you're now using the latexwrapper[] array. (This
solves the reusability problem, by the way, although you still face the
buffer overflow and quadratic time issues.)

Others have mentioned the possibility of reading this block of text
from a file instead of burying it in the program code. Although that
gives up some degree of self-containment, it brings some advantages.
For example, if you decide you'd like 12-point instead of 10-point
titles you can just edit the external file without rebuilding the
program from source -- if you're an end user, you might not even have
access to the source. Don't reject the idea out-of-hand.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Kaz Kylheku 12-23-2011 04:48 PM

Re: warning: string length greater than '509'
 
On 2011-12-23, JohnF <john@please.see.sig.for.email.com> wrote:
> I like to compile (linux/gcc) with -pedantic -Wall
> and neaten up the code to eliminate all warnings.
> But the following code emits the message
> warning: string length '1008' is greater than the
> length '509' ISO C90 compilers are required to support
> and I can't see a neat way around it.


Doh, you asked for pedantic and so you got it!

> only solution. But a straightforward way around the
> warning would be even nicer.


Delete "-pedantic" from the compiler command line.

Seebs 12-23-2011 07:04 PM

Re: warning: string length greater than '509'
 
On 2011-12-23, Robert Wessel <robertwessel2@yahoo.com> wrote:
> But while C89/C90 has been officially superseded by C99 (or actually
> C11 as of a couple of weeks ago), there are a significant number of
> compilers in the world that have not fully implemented C99 (or any of
> C99) while still providing a full C89 implementation. MS's standard
> compiler falls into that category, for example, as do many compilers
> for embedded targets.


Well, I'd be curious to know how many of them are in the "none at all"
category. In theory, gcc doesn't fully support C99, but I've never actually
run into anything I wanted that it didn't have.

I regularly use designated initializers and struct literals, and I don't think
I've ever had a problem with that. Of course, none of what I write has to
run on Windows. :)

As to embedded targets... Obviously I'm biased as I'm mostly working with
Linux these days, but it's been quite a while since I saw an embedded target
vendor not provide gcc for their target. Maintaining your own compiler is
a lot more work.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.


All times are GMT. The time now is 09:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.