Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why does this dump core?

Reply
Thread Tools

Why does this dump core?

 
 
Army1987
Guest
Posts: n/a
 
      05-11-2007
What's wrong with this code?

#define DEBUG 1
#include <string.h>
#if DEBUG
#include <stdio.h>
#endif
#include <stdlib.h>
#define MAX_LEN 32767
int main(int argc, char **argv)
{
char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
size_t len = strlen(line);
while (++argv && len < MAX_LEN) {
strncat(line, " ", MAX_LEN - len - 1);
len++;
strncat(line, *argv, MAX_LEN - len - 1);
len += strlen(*argv);
}
#if DEBUG
puts(line);
return 0;
#endif
return system(line);
}


--
char s[]="\16Jsa ukenethr ,cto haCr\n";int main(void){*s*=5;*
s%=23;putchar(s[0][s]);return*s-14?main():!putchar(9[s+*s]);}


 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      05-12-2007
On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <(E-Mail Removed)>
wrote:

>What's wrong with this code?
>
>#define DEBUG 1
>#include <string.h>
>#if DEBUG
> #include <stdio.h>
>#endif
>#include <stdlib.h>
>#define MAX_LEN 32767
>int main(int argc, char **argv)
>{
> char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
> size_t len = strlen(line);
> while (++argv && len < MAX_LEN) {


You need to test *argv for NULL.

> strncat(line, " ", MAX_LEN - len - 1);
> len++;
> strncat(line, *argv, MAX_LEN - len - 1);
> len += strlen(*argv);
> }
>#if DEBUG
> puts(line);
> return 0;
>#endif
> return system(line);
>}



Remove del for email
 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      05-12-2007

"Barry Schwarz" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed)...
> On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <(E-Mail Removed)>
> wrote:
>
>>What's wrong with this code?
>>
>>#define DEBUG 1
>>#include <string.h>
>>#if DEBUG
>> #include <stdio.h>
>>#endif
>>#include <stdlib.h>
>>#define MAX_LEN 32767
>>int main(int argc, char **argv)
>>{
>> char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
>> size_t len = strlen(line);
>> while (++argv && len < MAX_LEN) {

>
> You need to test *argv for NULL.


Yes. I replaced it with while (*++argv && len < MAX_LEN) and it
works. Of course argv itself will never become NULL. I wonder how
could I repeatedly fail to see that. I had even suspected that forý
some reasons one cannot assign to argv...


 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      05-14-2007
Barry Schwarz wrote:
>
> On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <(E-Mail Removed)>
> wrote:
>
> >What's wrong with this code?

[...]
> >int main(int argc, char **argv)
> >{
> > char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
> > size_t len = strlen(line);
> > while (++argv && len < MAX_LEN) {

>
> You need to test *argv for NULL.

[...]

Does the standard guarantee that argv[argc]==NULL (I know that the
implementations I use do add a NULL to the argv[] array), or do you
need to make sure you stop at argv[argc-1] ?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      05-14-2007

"Army1987" <(E-Mail Removed)> wrote in message
news:f246kf$lag$(E-Mail Removed)...
>
> "Barry Schwarz" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed)...
>> On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <(E-Mail Removed)>
>> wrote:
>>
>>>What's wrong with this code?
>>>
>>>#define DEBUG 1
>>>#include <string.h>
>>>#if DEBUG
>>> #include <stdio.h>
>>>#endif
>>>#include <stdlib.h>
>>>#define MAX_LEN 32767
>>>int main(int argc, char **argv)
>>>{
>>> char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
>>> size_t len = strlen(line);
>>> while (++argv && len < MAX_LEN) {

>>
>> You need to test *argv for NULL.

>
> Yes. I replaced it with while (*++argv && len < MAX_LEN) and it
> works.

No, it doesn't. You are not checking whether argv is NULL.
Youi are incrementing it and THEN checking for NULL.

> Of course argv itself will never become NULL.


Sure it can. One way is this:

int main( int argc, char **argv ) {
if ( argv!=NULL) {
main(0, NULL);
}
return 0;
}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-14-2007
Kenneth Brody wrote:

> Barry Schwarz wrote:
> >
> > On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <(E-Mail Removed)>
> > wrote:
> >
> > > What's wrong with this code?

> [...]
> > > int main(int argc, char **argv)
> > > {
> > > char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
> > > size_t len = strlen(line);
> > > while (++argv && len < MAX_LEN) {

> >
> > You need to test *argv for NULL.

> [...]
>
> Does the standard guarantee that argv[argc]==NULL


Yes. From the c99 draft:

[#2] If they are declared, the parameters to the main
function shall obey the following constraints:

-- The value of argc shall be nonnegative.

-- argv[argc] shall be a null pointer.


Brian
 
Reply With Quote
 
Coos Haak
Guest
Posts: n/a
 
      05-14-2007
Op Mon, 14 May 2007 13:41:47 -0400 schreef Kenneth Brody:

> Barry Schwarz wrote:
>>
>> On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <(E-Mail Removed)>
>> wrote:
>>
>>>What's wrong with this code?

> [...]
>>>int main(int argc, char **argv)
>>>{
>>> char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
>>> size_t len = strlen(line);
>>> while (++argv && len < MAX_LEN) {

>>
>> You need to test *argv for NULL.

> [...]
>
> Does the standard guarantee that argv[argc]==NULL (I know that the
> implementations I use do add a NULL to the argv[] array), or do you
> need to make sure you stop at argv[argc-1] ?


Yes, n1124.pdf, paragraph 5.1.2.2.1 Program startup states
°X argv[argc] shall be a null pointer.

--
Coos
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      05-24-2007

"Fred Kleinschmidt" <(E-Mail Removed)> ha scritto nel
messaggio news:(E-Mail Removed)...
>
> "Army1987" <(E-Mail Removed)> wrote in message
> news:f246kf$lag$(E-Mail Removed)...
>>
>> "Barry Schwarz" <(E-Mail Removed)> ha scritto nel messaggio
>> news:(E-Mail Removed)...
>>> On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <(E-Mail Removed)>
>>> wrote:
>>>
>>>>What's wrong with this code?
>>>>
>>>>#define DEBUG 1
>>>>#include <string.h>
>>>>#if DEBUG
>>>> #include <stdio.h>
>>>>#endif
>>>>#include <stdlib.h>
>>>>#define MAX_LEN 32767
>>>>int main(int argc, char **argv)
>>>>{
>>>> char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
>>>> size_t len = strlen(line);
>>>> while (++argv && len < MAX_LEN) {
>>>
>>> You need to test *argv for NULL.

>>
>> Yes. I replaced it with while (*++argv && len < MAX_LEN) and it
>> works.

> No, it doesn't. You are not checking whether argv is NULL.
> Youi are incrementing it and THEN checking for NULL.


So? argv looks like this:

argv
\/
|ptr. to program name|ptr. to 1st arg|...|ptr. to last arg|NULL|
After ptr. to last argument has been used, argv is increased, then
checked for NULL. Since it is NULL, the loop breaks. This is
exactly what I was trying to do.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dump complete java VM state as core dump (not via OS) possible? halfdog Java 12 02-21-2013 06:14 AM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments