Script started on Fri 20 Apr 2018 07:49:27 PM PDT kmwinst@klaatu:~/lecture11$ ./reverse Please enter a string: foo Segmentation fault kmwinst@klaatu:~/lecture11\klaatu:~/lecture11$ gdb reverse GNU gdb (GDB) Red Hat Enterprise Linux 7.11-67.el7 Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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-redhat-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from reverse...done. (gdb) run Starting program: /homes/mwinst/lecture11/reverse Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64 [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Please enter a string: foo Program received signal SIGSEGV, Segmentation fault. 0x00007ffff764f710 in __strcpy_sse2 () from /lib64/libc.so.6 Missing separate debuginfos, use: debuginfo-install libgcc-4.8.5-16.el7_4.2.x86_64 libstdc++-4.8.5-16.el7_4.2.x86_64 zlib-1.2.7-17.el7.x86_64 (gdb) backtrace #0 0x00007ffff764f710 in __strcpy_sse2 () from /lib64/libc.so.6 #1 0x00000000004006bd in reverse (s=0x7fffffffdcb0 "foo\n") at reverse.c:19 #2 0x000000000040076b in main () at reverse.c:41 (gdb) bt #0 0x00007ffff764f710 in __strcpy_sse2 () from /lib64/libc.so.6 #1 0x00000000004006bd in reverse (s=0x7fffffffdcb0 "foo\n") at reverse.c:19 #2 0x000000000040076b in main () at reverse.c:41 (gdb) up #1 0x00000000004006bd in reverse (s=0x7fffffffdcb0 "foo\n") at reverse.c:19 19 strcpy(result, s); (gdb) list 14 char * result = NULL; /* the reversed string */ 15 int L, R; 16 char ch; 17 18 /* copy original string then reverse and return the copy */ 19 strcpy(result, s); 20 21 L = 0; 22 R = strlen(result); 23 while (L < R) { (gdb) up #2 0x000000000040076b in main () at reverse.c:41 41 rev_line = reverse(line); (gdb) list 36 char line[MAX_STR]; /* original input line */ 37 char * rev_line; /* backwards copy from reverse function */ 38 39 printf("Please enter a string: "); 40 fgets(line, MAX_STR, stdin); 41 rev_line = reverse(line); 42 printf("The original string was: >%s<\n", line); 43 printf("Backwards, that string is: >%s<\n", rev_line); 44 printf("Thank you for trying our program.\n"); 45 return EXIT_SUCCESS; (gdb) print line $1 = "foo\n\000\000\000\000\030\000\000\000\001", '\000' , "\001\000\000\000\000\000\000\000\375\a@", '\000' , "\260\a@\000\000\000\000\000\240\005@\000\000\000\000\000\000\336\377\377" (gdb) p line $2 = "foo\n\000\000\000\000\030\000\000\000\001", '\000' , "\001\000\000\000\000\000\000\000\375\a@", '\000' , "\260\a@\000\000\000\000\000\240\005@\000\000\000\000\000\000\336\377\377" (gdb) down #1 0x00000000004006bd in reverse (s=0x7fffffffdcb0 "foo\n") at reverse.c:19 19 strcpy(result, s); (gdb) p s $3 = 0x7fffffffdcb0 "foo\n" (gdb) p result $4 = 0x0 (gdb) p L $5 = 32767 (gdb) p R $6 = -8928 (gdb) info locals result = 0x0 L = 32767 R = -8928 ch = 0 '\000' (gdb) quit A debugging session is active. Inferior 1 [process 20734] will be killed. Quit anyway? (y or n) y kmwinst@klaatu:~/lecture11\klaatu:~/lecture11$ emacs reverse.c kmwinst@klaatu:~/lecture11\klaatu:~/lecture11$ gcc -Wall -std=c11 -g -o reverse reverse.c kmwinst@klaatu:~/lecture11\klaatu:~/lecture11$ ./reverse Please enter a string: foo The original string was: >foo < Backwards, that string is: >< Thank you for trying our program. kmwinst@klaatu:~/lecture11\klaatu:~/lecture11$ gdb reverse GNU gdb (GDB) Red Hat Enterprise Linux 7.11-67.el7 Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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-redhat-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from reverse...done. (gdb) break reverse Breakpoint 1 at 0x4006a6: file reverse.c, line 18. (gdb) run Starting program: /homes/mwinst/lecture11/reverse Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64 [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Please enter a string: b foo Breakpoint 1, reverse (s=0x7fffffffdcb0 "foo\n", result=0x7fffffffdc40 "") at reverse.c:18 18 strcpy(result, s); Missing separate debuginfos, use: debuginfo-install libgcc-4.8.5-16.el7_4.2.x86_64 libstdc++-4.8.5-16.el7_4.2.x86_64 zlib-1.2.7-17.el7.x86_64 (gdb) bt #0 reverse (s=0x7fffffffdcb0 "foo\n", result=0x7fffffffdc40 "") at reverse.c:18 #1 0x0000000000400774 in main () at reverse.c:41 (gdb) list 13 char * reverse(char * s, char * result) { 14 int L, R; 15 char ch; 16 17 /* copy original string then reverse and return the copy */ 18 strcpy(result, s); 19 20 L = 0; 21 R = strlen(result); 22 while (L < R) { (gdb) p s $1 = 0x7fffffffdcb0 "foo\n" (gdb) p strlen(s) $2 = 4 (gdb) step 20 L = 0; (gdb) p result $3 = 0x7fffffffdc40 "foo\n" (gdb) step 21 R = strlen(result); (gdb) step 22 while (L < R) { (gdb) p L $4 = 0 (gdb) p R $5 = 4 (gdb) list 17 /* copy original string then reverse and return the copy */ 18 strcpy(result, s); 19 20 L = 0; 21 R = strlen(result); 22 while (L < R) { 23 ch = result[L]; 24 result[L] = result[R]; 25 result[R] = ch; 26 L++; R--; (gdb) step 23 ch = result[L]; (gdb) step 24 result[L] = result[R]; (gdb) p ch $6 = 102 'f' (gdb) p result[R] $7 = 0 '\000' (gdb) quit A debugging session is active. Inferior 1 [process 20972] will be killed. Quit anyway? (y or n) y kmwinst@klaatu:~/lecture11\klaatu:~/lecture11$ emacs reverse.c kmwinst@klaatu:~/lecture11\klaatu:~/lecture11$ gcc -Wall -std=c11 -g -o reverse reverse.c kmwinst@klaatu:~/lecture11\klaatu:~/lecture11$ ./reverse Please enter a string: foo The original string was: >foo < Backwards, that string is: > oof< Thank you for trying our program. kmwinst@klaatu:~/lecture11\klaatu:~/lecture11$ exit Script done on Fri 20 Apr 2018 07:54:12 PM PDT