Thread: Segmentation error in programm

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    5

    Unhappy Segmentation error in programm

    I'm a complete noob and I need help! I like everything written correctly, here's the code:
    Code:
    #include <stdio.h>
    #include "iwlib.h"
    
    int set_essid(int    skfd, char ifname[], char arg[], int count);
    
    int main(int argc, char *argv[])
    {
      int skfd;
      if((skfd = iw_sockets_open()) < 0)
      {
        perror("socket");
        exit(-1);
      }
      
      if(argc != 3) {
        printf("USING: %s [FACE] [essid]\n", argv[0]);
        goto quit;
      }
      
      if( set_essid(skfd, argv[2], argv[3], 0) == 0 ) {
        printf("OK!\n");
      }
      else {
        printf("ERROR!\n");
        perror("iw_set_ext");
      }
      
      quit:
      iw_sockets_close(skfd);
      return 0;
    }
    
    int set_essid(int    skfd, char ifname[], char arg[], int count)
    {
      struct iwreq wrq;
      char essid[IW_ESSID_MAX_SIZE + 1];
      int we_kernel_version;
      
      
      wrq.u.essid.flags = 1;
      strcpy(essid, arg);
      
      we_kernel_version = iw_get_kernel_we_version();
      
      wrq.u.essid.pointer = (caddr_t) essid;
      wrq.u.essid.length = strlen(essid);
      if(we_kernel_version < 21)
        wrq.u.essid.length++;
    
      if(iw_set_ext(skfd, ifname, SIOCSIWESSID, &wrq) < 0)
        return(-1);
      
      return 0;
    }
    I compiled this way
    Code:
    gcc libiw.so.29 main.c -lm -Wall
    At startup gives an error:
    Code:
    user $ ./a.out wifi d
    Segmentation error (the memory stack is flushed to disk)
    Help me!!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This is where learning how to use a debugger comes in handy.
    Code:
    $ gcc -g foo.c -liw
    $ ./a.out wifi d
    Segmentation fault (core dumped)
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) run wifi d
    Starting program: /home/salem/a.out wifi d
    
    Program received signal SIGSEGV, Segmentation fault.
    __strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:296
    296	../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.
    (gdb) bt
    #0  __strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:296
    #1  0x0000000000400af1 in set_essid (skfd=3, ifname=0x7fffffffe265 "d", arg=0x0, count=0) at foo.c:41
    #2  0x0000000000400a75 in main (argc=3, argv=0x7fffffffde98) at foo.c:20
    (gdb) frame 1
    #1  0x0000000000400af1 in set_essid (skfd=3, ifname=0x7fffffffe265 "d", arg=0x0, count=0) at foo.c:41
    41	  strcpy(essid, arg);
    (gdb) frame 2
    #2  0x0000000000400a75 in main (argc=3, argv=0x7fffffffde98) at foo.c:20
    20	  if( set_essid(skfd, argv[2], argv[3], 0) == 0 ) {
    (gdb) print argv[0]
    $1 = 0x7fffffffe23d "/home/salem/a.out"
    (gdb) print argv[1]
    $2 = 0x7fffffffe260 "wifi"
    (gdb) print argv[2]
    $3 = 0x7fffffffe265 "d"
    It looks like you mis-counted your arguments.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    remember you're working off a zero base .. 0 = program name 1 = next item 2 = next item check with argc for length on cli

    Code:
    int main(int argc, char **argv)
    {
    
    if ( argc != 2  )
    {
        printf("error_message"\n);
        exit(1);
    }
    ..........
    
    return 0;
    }
    Last edited by userxbw; 09-26-2017 at 06:56 PM.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Please do not use goto at all.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    5
    Quote Originally Posted by Salem View Post
    This is where learning how to use a debugger comes in handy.
    Code:
    $ gcc -g foo.c -liw
    $ ./a.out wifi d
    Segmentation fault (core dumped)
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) run wifi d
    Starting program: /home/salem/a.out wifi d
    
    Program received signal SIGSEGV, Segmentation fault.
    __strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:296
    296    ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.
    (gdb) bt
    #0  __strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:296
    #1  0x0000000000400af1 in set_essid (skfd=3, ifname=0x7fffffffe265 "d", arg=0x0, count=0) at foo.c:41
    #2  0x0000000000400a75 in main (argc=3, argv=0x7fffffffde98) at foo.c:20
    (gdb) frame 1
    #1  0x0000000000400af1 in set_essid (skfd=3, ifname=0x7fffffffe265 "d", arg=0x0, count=0) at foo.c:41
    41      strcpy(essid, arg);
    (gdb) frame 2
    #2  0x0000000000400a75 in main (argc=3, argv=0x7fffffffde98) at foo.c:20
    20      if( set_essid(skfd, argv[2], argv[3], 0) == 0 ) {
    (gdb) print argv[0]
    $1 = 0x7fffffffe23d "/home/salem/a.out"
    (gdb) print argv[1]
    $2 = 0x7fffffffe260 "wifi"
    (gdb) print argv[2]
    $3 = 0x7fffffffe265 "d"
    It looks like you mis-counted your arguments.
    Ah that's the problem, the whole program works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error stray 342 in programm
    By Nantia Atzo in forum C Programming
    Replies: 8
    Last Post: 04-14-2016, 03:06 AM
  2. Basic Programm giving Error
    By dharmil007 in forum C Programming
    Replies: 16
    Last Post: 05-27-2010, 02:01 PM
  3. Segmentation Error / Bus Error in ANSI
    By drag0n69 in forum C Programming
    Replies: 10
    Last Post: 02-05-2008, 09:45 AM
  4. Simple programm error
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 09-15-2002, 09:09 PM
  5. What is a segmentation error?
    By Crankit211 in forum C Programming
    Replies: 3
    Last Post: 12-11-2001, 01:08 AM

Tags for this Thread