Skip to content

Instantly share code, notes, and snippets.

@ohmree
Last active March 20, 2017 18:32
Show Gist options
  • Save ohmree/954bc1b01b758f041a3ff341bd3655e3 to your computer and use it in GitHub Desktop.
Save ohmree/954bc1b01b758f041a3ff341bd3655e3 to your computer and use it in GitHub Desktop.
Why doesn't this work?
omrisim210:~/workspace $ gcc -Wall -Wextra -ggdb3 -O0 -std=gnu11 -v -da -Q stack-overflow.c -Iinclude -o so.o -lcjson
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.3' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
COLLECT_GCC_OPTIONS='-Wall' '-Wextra' '-ggdb3' '-O0' '-std=gnu11' '-v' '-da' '-Q' '-I' 'include' '-o' 'so.o' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/cc1 -v -I include -imultiarch x86_64-linux-gnu -dD stack-overflow.c -dumpbase stack-overflow.c -da -mtune=generic -march=x86-64 -auxbase stack-overflow -ggdb3 -O0 -Wall -Wextra -std=gnu11 -version -fstack-protector -Wformat-security -o /tmp/ccUhbgTU.s
GNU C (Ubuntu 4.8.4-2ubuntu1~14.04.3) version 4.8.4 (x86_64-linux-gnu)
compiled by GNU C version 4.8.4, GMP version 5.1.3, MPFR version 3.1.2-p3, MPC version 1.0.1
warning: MPFR header version 3.1.2-p3 differs from library version 3.1.3.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/4.8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
GNU C (Ubuntu 4.8.4-2ubuntu1~14.04.3) version 4.8.4 (x86_64-linux-gnu)
compiled by GNU C version 4.8.4, GMP version 5.1.3, MPFR version 3.1.2-p3, MPC version 1.0.1
warning: MPFR header version 3.1.2-p3 differs from library version 3.1.3.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
options passed: -v -I include -imultiarch x86_64-linux-gnu
stack-overflow.c -mtune=generic -march=x86-64 -ggdb3 -O0 -Wall -Wextra
-std=gnu11 -fstack-protector -Wformat-security
options enabled: -faggressive-loop-optimizations
-fasynchronous-unwind-tables -fauto-inc-dec -fbranch-count-reg -fcommon
-fdelete-null-pointer-checks -fdwarf2-cfi-asm -fearly-inlining
-feliminate-unused-debug-types -ffunction-cse -fgcse-lm -fgnu-runtime
-fgnu-unique -fident -finline-atomics -fira-hoist-pressure
-fira-share-save-slots -fira-share-spill-slots -fivopts
-fkeep-static-consts -fleading-underscore -fmath-errno
-fmerge-debug-strings -fmove-loop-invariants -fpeephole
-fprefetch-loop-arrays -freg-struct-return -fsched-critical-path-heuristic
-fsched-dep-count-heuristic -fsched-group-heuristic -fsched-interblock
-fsched-last-insn-heuristic -fsched-rank-heuristic -fsched-spec
-fsched-spec-insn-heuristic -fsched-stalled-insns-dep -fshow-column
-fsigned-zeros -fsplit-ivs-in-unroller -fstack-protector
-fstrict-volatile-bitfields -fsync-libcalls -ftrapping-math
-ftree-coalesce-vars -ftree-cselim -ftree-forwprop -ftree-loop-if-convert
-ftree-loop-im -ftree-loop-ivcanon -ftree-loop-optimize
-ftree-parallelize-loops= -ftree-phiprop -ftree-pta -ftree-reassoc
-ftree-scev-cprop -ftree-slp-vectorize -ftree-vect-loop-version
-funit-at-a-time -funwind-tables -fvar-tracking -fvar-tracking-assignments
-fzero-initialized-in-bss -m128bit-long-double -m64 -m80387
-maccumulate-outgoing-args -malign-stringops -mfancy-math-387
-mfp-ret-in-387 -mfxsr -mglibc -mieee-fp -mlong-double-80 -mmmx -mno-sse4
-mpush-args -mred-zone -msse -msse2 -mtls-direct-seg-refs
Compiler executable checksum: a0a649d344b1ed798e33d30772d46437
__bswap_32 __bswap_64 main
Analyzing compilation unit
Performing interprocedural optimizations
<*free_lang_data> <visibility> <early_local_cleanups> <*free_inline_summary> <whole-program>Assembling functions:
main
Execution times (seconds)
phase setup : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 (20%) wall 1108 kB (44%) ggc
phase parsing : 0.02 (100%) usr 0.01 (50%) sys 0.03 (60%) wall 1217 kB (49%) ggc
phase opt and generate : 0.00 ( 0%) usr 0.01 (50%) sys 0.01 (20%) wall 46 kB ( 2%) ggc
dump files : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 (20%) wall 0 kB ( 0%) ggc
preprocessing : 0.01 (50%) usr 0.01 (50%) sys 0.02 (40%) wall 418 kB (17%) ggc
lexical analysis : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 (20%) wall 0 kB ( 0%) ggc
parser (global) : 0.01 (50%) usr 0.00 ( 0%) sys 0.00 ( 0%) wall 564 kB (23%) ggc
integrated RA : 0.00 ( 0%) usr 0.01 (50%) sys 0.00 ( 0%) wall 12 kB ( 1%) ggc
TOTAL : 0.02 0.02 0.05 2503 kB
COLLECT_GCC_OPTIONS='-Wall' '-Wextra' '-ggdb3' '-O0' '-std=gnu11' '-v' '-da' '-Q' '-I' 'include' '-o' 'so.o' '-mtune=generic' '-march=x86-64'
as -v -I include --64 -o /tmp/cc8DD5Ng.o /tmp/ccUhbgTU.s
GNU assembler version 2.24 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.24
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-Wall' '-Wextra' '-ggdb3' '-O0' '-std=gnu11' '-v' '-da' '-Q' '-I' 'include' '-o' 'so.o' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o so.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. /tmp/cc8DD5Ng.o -lcjson -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
omrisim210:~/workspace $ gdb so.o
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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:
---Type <return> to continue, or q <return> to quit---
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from so.o...done.
(gdb) run
Starting program: /home/ubuntu/workspace/so.o
Program received signal SIGSEGV, Segmentation fault.
0x00000000004007be in main () at stack-overflow.c:14
14 char* joke = cJSON_GetObjectItem(root, "value")->valuestring;
(gdb) where
#0 0x00000000004007be in main () at stack-overflow.c:14
(gdb) list
9 \"id\" : \"InlyL_TsToSc9em4KAr8bw\",\
10 \"url\" : \"http://api.chucknorris.io/jokes/InlyL_TsToSc9em4KAr8bw\"\
11 \"value\" : \"The ONLY Chuck Norris regret.... inventing Al Gore.\"\
12 }";
13 cJSON* root = cJSON_Parse(jsonstr);
14 char* joke = cJSON_GetObjectItem(root, "value")->valuestring;
15 printf("heres a joke:\n %s\n", joke);
16 cJSON_Delete(root);
17 return EXIT_SUCCESS;
18 }
(gdb) print root
$1 = (cJSON *) 0x0
(gdb) print jsonstr
$2 = 0x400878 "{ \"icon_url\" : \"https://assets.chucknorris.host/img/avatar/chuck-norris.png\", \"id\" : \"InlyL_TsToSc9em4KAr8bw\", \"url\" : \"http://api.chucknorris.io/jokes/InlyL_TsToSc9em4KAr8bw\" "...
(gdb) print joke
$3 = 0x0
(gdb) pt jsonstr
type = const char *
(gdb) pt root
type = struct cJSON {
struct cJSON *next;
struct cJSON *prev;
struct cJSON *child;
int type;
char *valuestring;
int valueint;
double valuedouble;
char *string;
} *
(gdb) pt joke
type = char *
(gdb) quit
A debugging session is active.
Inferior 1 [process 48127] will be killed.
Quit anyway? (y or n) y
omrisim210:~/workspace $
#include <stdio.h>
#include <stdlib.h>
#include <cjson/cJSON.h>
int main() {
const char* jsonstr = "{\
\"icon_url\" : \"https://assets.chucknorris.host/img/avatar/chuck-norris.png\",\
\"id\" : \"InlyL_TsToSc9em4KAr8bw\",\
\"url\" : \"http://api.chucknorris.io/jokes/InlyL_TsToSc9em4KAr8bw\"\
\"value\" : \"The ONLY Chuck Norris regret.... inventing Al Gore.\"\
}";
cJSON* root = cJSON_Parse(jsonstr);
char* joke = cJSON_GetObjectItem(root, "value")->valuestring;
printf("heres a joke:\n %s\n", joke);
cJSON_Delete(root);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment