Thread: Abort Trap Error

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Abort Trap Error

    I'm trying to make my own cd command. When I try to run :

    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    
      char path[sizeof(argv)];
    
     if(  strcpy(path, argv[1]) < 0)
      printf("error converting argv to string\n");
     else
       printf("argv converted to string\n");
     
     if (argc != 2)
        printf("Input error");
      else
     
     if (chdir(path) < 0)
        printf("FAILURE\n");
      else
        printf("SUCCESS\n");
      
    
    char cwd[1024];
      if (getcwd(cwd, sizeof(cwd)) != NULL)
        fprintf(stdout, "You are in %s\n", cwd);
    
      exit(0);
    }
    It works for any existing subdirectory and ".."(as in move up one level).

    However if I try to cd to any directory that does not exist or any directory that is above the current directory, I get the error Abort Trap. I have no idea what that means, this error is not listed in the man pages for either strcpy() or chdir() which is where i would assume the error to be happening.

    Any help will be appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    char path[sizeof(argv)];
    This doesn't do what you think it's doing. argv is effectively a pointer to a pointer, the size of which is the size of any pointer (32 bits on a 32-bit system, 64 on a 64, etc). You want something reasonable, like 256 or something. Here's a tiny bit of info about the signal you're getting: SIGABRT - Wikipedia, the free encyclopedia.

    It could be because you overflow path and scramble important stack info for returning from main. When you return from main, you back into some basic startup code that launched your process to begin with, and this may be what's broken. Oh, and you should return 0 from main, not exit(0).

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummmm... Why don't you just use chdir(argv[1]); ?

    There's no real reason to copy it, unless you are modifying it... and doing it this way gets rid of all the code above your "if (argc < 2)... " line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM