2

I have the following piece of code, compiled with gcc `pkg-config --cflags glib-2.0` test.c -o test the code works, but only when I use gcc `pkg-config --cflags --libs glib-2.0` test.c -o test the code gets segfault when running.

#include <glib.h>     

typedef struct _mystruct{
    int a;
}my;                                                                         
                                                                                               
static void set_int(my *mert){                                                                 
  mert->a = 5;                                                                                   
}                                                                                                                                                                                                                                                                                        
                                                                                           
int main() {                                                                                                                                                        
  my *myInt;                                                                                                                                                                             
  set_int(myInt);                                                                                                                                                                    
}                                 

I tried with GDB, when the program is compiled with --libs the segfault comes from here

0x0000000000401132 in set_int (mert=0x0) at test.c:5
5       mert->a = 5;

But when it is compiled only with --cflags all is on its place

Breakpoint 1, set_int (mert=0x7ffff7ffdab0 <_rtld_local+2736>) at test.c:5
5       mert->a = 5;

I have also tried with gcc `pkg-config --cflags glib-2.0` test.c -o test `pkg-config --libs glib-2.0` but it doesn't change anything.

  • 3
    You never initialize `myInt` so the expression `mert->a = 5` in `set_int` will lead to undefined behaviour -- pure and simple. Nothing to do with `pkg-config` I'm afraid. – G.M. Dec 26 '22 at 16:53
  • 1
    @G.M There is a log of GDB without using --libs and that line of code is working, I only get segfault when using --libs and there is also the log of that in the post – nevermindmyname Dec 26 '22 at 16:55
  • 1
    I think the compiler should also give you a warning that you are trying to access data through an uninitialized pointer. – Pablo Dec 26 '22 at 17:18
  • 3
    @nevermindmyname the advice that GM gave you is correct. No matter how you build, you can't depend on that uninitialized pointer to work right -- give it a value with malloc() or something similar. – mzimmers Dec 26 '22 at 17:22
  • 3
    *There is a log of GDB without using --libs and that line of code is working* No, it was **never** "working". Undefined behavior from dereferencing an uninitialized pointer never "works". It just may not blow up and fail in an observable manner. – Andrew Henle Dec 26 '22 at 17:54

1 Answers1

3

myInt is an uninitialized pointer and it happens to be NULL in the case where your program is segfault'ing. You pointer needs to reference a struct as set_int() dereferences said pointer:

int main() {             
  my myInt;                            
  set_int(&myInt);
}

Or you could could zero initialize your struct and take it's address if you want myInt to be a pointer:

int main() {
  my *myInt = &(my) { 0 };
  set_int(myInt);
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38