Segmentation fault on return from sub-routine
by StrayBit from LinuxQuestions.org on (#5AF98)
Given this functionCode:/* whichfltyp.c
* displays FileTp structure
* Bruce Irving 10/5/2020
*/
#include <stdio.h>
// #include <stdlib.h>
#include <string.h>
// #include <brlib.h>
#include "mysrch.h"
static struct FileTp
{ int ftn;
char ftd[10];
}/*;
static struct FileTp*/ fltyp[FTMAX+1] =
{ {0x0, "unknown"}, // 000 0
{0x1, "log"}, // 001 1
{0x2, "db"}, // 010 2
{0x4, "backup"}, // 100 4
{0x6, "dbbkp"}, // 110 6
{0xf, "all"} // 111 f
};
enum FTpSel { unknown, log = 1, db = 2, backup = 4, dbbkp = 6, all = 15 };
// displays description of ftp
char *whichfltyp( int ftp )
{ int i;
static char rt[10] ="";
if( ftp > FTMAX )
return NULL;
for( i = 0; i <= FTMAX; i++ )
if( fltyp[i].ftn == ftp )
break;
strcpy( rt, fltyp[i].ftd ); // to avoid accidental change to structure
return rt;
} // whichfltyp()and debuggingCode:bruce@pop-os:~/bin/src/mysrch$ gdb ./gnusrch
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./gnusrch...
(gdb) b fillpipe.c:89
Breakpoint 1 at 0x17c7: file fillpipe.c, line 89.
(gdb) r
Starting program: /home/bruce/bin/src/mysrch/gnusrch
[Detaching after vfork from child process 1307176]
Breakpoint 1, fillpipe () at fillpipe.c:89
warning: Source file is more recent than executable.
89 brtracef( "fillpipe %d: %3d: %s%s is %s\n",
(gdb) s
whichfltyp (ftp=21845) at whichfltyp.c:31
31 { int i;
(gdb) n
34 if( ftp > FTMAX )
(gdb) n
36 for( i = 0; i <= FTMAX; i++ )
(gdb) n
37 if( fltyp[i].ftn == ftp )
(gdb) n
36 for( i = 0; i <= FTMAX; i++ )
(gdb) n
37 if( fltyp[i].ftn == ftp )
(gdb) n
36 for( i = 0; i <= FTMAX; i++ )
(gdb) n
37 if( fltyp[i].ftn == ftp )
(gdb) p i
$1 = 2
(gdb) p ftp
$2 = 2
(gdb) n
38 break;
(gdb) n
39 strcpy( rt, fltyp[i].ftd ); // to avoid accidental change to structure
(gdb) p fltyp[i].ftd
$3 = "db\000\000\000\000\000\000\000"
(gdb) n
40 return rt;
(gdb) n
41 } // whichfltyp()
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:96
96 ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
(gdb)Program received signal SIGSEGV, Segmentation fault.
__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:96
96../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
I don't know why it would be calling strlen at this point.
I'm at a loss as to how to proceed. Any suggestions appreciated.
Bruce


* displays FileTp structure
* Bruce Irving 10/5/2020
*/
#include <stdio.h>
// #include <stdlib.h>
#include <string.h>
// #include <brlib.h>
#include "mysrch.h"
static struct FileTp
{ int ftn;
char ftd[10];
}/*;
static struct FileTp*/ fltyp[FTMAX+1] =
{ {0x0, "unknown"}, // 000 0
{0x1, "log"}, // 001 1
{0x2, "db"}, // 010 2
{0x4, "backup"}, // 100 4
{0x6, "dbbkp"}, // 110 6
{0xf, "all"} // 111 f
};
enum FTpSel { unknown, log = 1, db = 2, backup = 4, dbbkp = 6, all = 15 };
// displays description of ftp
char *whichfltyp( int ftp )
{ int i;
static char rt[10] ="";
if( ftp > FTMAX )
return NULL;
for( i = 0; i <= FTMAX; i++ )
if( fltyp[i].ftn == ftp )
break;
strcpy( rt, fltyp[i].ftd ); // to avoid accidental change to structure
return rt;
} // whichfltyp()and debuggingCode:bruce@pop-os:~/bin/src/mysrch$ gdb ./gnusrch
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./gnusrch...
(gdb) b fillpipe.c:89
Breakpoint 1 at 0x17c7: file fillpipe.c, line 89.
(gdb) r
Starting program: /home/bruce/bin/src/mysrch/gnusrch
[Detaching after vfork from child process 1307176]
Breakpoint 1, fillpipe () at fillpipe.c:89
warning: Source file is more recent than executable.
89 brtracef( "fillpipe %d: %3d: %s%s is %s\n",
(gdb) s
whichfltyp (ftp=21845) at whichfltyp.c:31
31 { int i;
(gdb) n
34 if( ftp > FTMAX )
(gdb) n
36 for( i = 0; i <= FTMAX; i++ )
(gdb) n
37 if( fltyp[i].ftn == ftp )
(gdb) n
36 for( i = 0; i <= FTMAX; i++ )
(gdb) n
37 if( fltyp[i].ftn == ftp )
(gdb) n
36 for( i = 0; i <= FTMAX; i++ )
(gdb) n
37 if( fltyp[i].ftn == ftp )
(gdb) p i
$1 = 2
(gdb) p ftp
$2 = 2
(gdb) n
38 break;
(gdb) n
39 strcpy( rt, fltyp[i].ftd ); // to avoid accidental change to structure
(gdb) p fltyp[i].ftd
$3 = "db\000\000\000\000\000\000\000"
(gdb) n
40 return rt;
(gdb) n
41 } // whichfltyp()
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:96
96 ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
(gdb)Program received signal SIGSEGV, Segmentation fault.
__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:96
96../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
I don't know why it would be calling strlen at this point.
I'm at a loss as to how to proceed. Any suggestions appreciated.
Bruce