Why does this dump core?

Discussion in 'C Programming' started by Army1987, May 12, 2007.

  1. Army1987

    Army1987 Guest

    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]);return*s-14?main():!putchar(9[s+*s]);}
     
    Army1987, May 12, 2007
    #1
    1. Advertising

  2. On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <>
    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
     
    Barry Schwarz, May 12, 2007
    #2
    1. Advertising

  3. Army1987

    Army1987 Guest

    "Barry Schwarz" <> ha scritto nel messaggio
    news:...
    > On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <>
    > 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...
     
    Army1987, May 12, 2007
    #3
  4. Barry Schwarz wrote:
    >
    > On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <>
    > 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: <mailto:>
     
    Kenneth Brody, May 14, 2007
    #4
  5. "Army1987" <> wrote in message
    news:f246kf$lag$...
    >
    > "Barry Schwarz" <> ha scritto nel messaggio
    > news:...
    >> On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <>
    >> 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
     
    Fred Kleinschmidt, May 14, 2007
    #5
  6. Army1987

    Default User Guest

    Kenneth Brody wrote:

    > Barry Schwarz wrote:
    > >
    > > On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <>
    > > 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
     
    Default User, May 14, 2007
    #6
  7. Army1987

    Coos Haak Guest

    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" <>
    >> 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
     
    Coos Haak, May 14, 2007
    #7
  8. Army1987

    Army1987 Guest

    "Fred Kleinschmidt" <> ha scritto nel
    messaggio news:...
    >
    > "Army1987" <> wrote in message
    > news:f246kf$lag$...
    >>
    >> "Barry Schwarz" <> ha scritto nel messaggio
    >> news:...
    >>> On Sat, 12 May 2007 01:58:38 +0200, "Army1987" <>
    >>> 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.
     
    Army1987, May 24, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. halfdog
    Replies:
    12
    Views:
    12,471
  2. Ng Pheng Siong

    What does this core dump mean?

    Ng Pheng Siong, Jul 5, 2003, in forum: Python
    Replies:
    2
    Views:
    435
    Ng Pheng Siong
    Jul 7, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,030
    Smokey Grindel
    Dec 2, 2006
  4. QQ

    Why do I have a core dump ?

    QQ, Jun 1, 2005, in forum: C Programming
    Replies:
    1
    Views:
    378
    -berlin.de
    Jun 1, 2005
  5. Andrew Musselman

    Where does core dump info go?

    Andrew Musselman, Mar 17, 2006, in forum: Ruby
    Replies:
    0
    Views:
    96
    Andrew Musselman
    Mar 17, 2006
Loading...

Share This Page