Search code examples
ctypedefnull-pointer

UndefinedBehaviorSanitizer becouse of nullpointer


Hej, I have some code in C, that dosent work. I am just trying to get a price from typedef potatoes to my check-out fuction. I "//" the code which is not in use. I there are needed more info please just ask, though I have no clue how to get pas this problem. So any help is much appreciated.

 #include <stdio.h>
 #include <string.h>   

    int number;

    void get_number ( );
    void show_menu ( );
    int get_input ( );
    void chech_out ();

    struct Product 
    {
            float potatoes;
    };
    typedef struct Product product;

    int main (void)
    {
        struct Product product;

        while (1)
        {
            //show_product1 ();
            show_menu ();
            if (get_input ()) // end on "a"
                break;
        }
        printf ("Bye!");
        return 0;
    }



    void show_menu (void)
    {
        printf ("Type what you wanne do");
        printf ("f)change amount    h)chech out    s)Strawberry");
        printf ("c)carrots    p)potatoes  o)Onion");
        printf ("a)quit");
    }

    void get_number ( )
    {

        printf ("Enter number in kg: ");
        scanf ("%d", &number);
    }



    int get_input (struct Product *product )
    {
        char letter;

        scanf ("%c", &letter);
        switch (letter)
        {
            case 'f':
                printf("\n\n\nWhenever you enter a new amount it will reset the old one!\n\n");

                break;
            case 'h':
                chech_out();


                break;
            case 'c':

                break;
            case 'p':


                get_number();           
                float P_freightPrice = number*12;
                float P_kgPrice = number*25;
                float P_totalprice=P_freightPrice+P_kgPrice;
                product->potatoes = P_totalprice;
           printf("\n%f\n",P_totalprice);     

         if (P_totalprice <= 100)
         {
           printf("%f\n",P_totalprice);    
         }
         if (P_totalprice>=101 && number<=350)
         {
             float New=(P_totalprice/100)*5;
             float total=P_totalprice-New;
             printf("%f\n",total);    
         }
         if(P_totalprice>=351 && P_totalprice<=600)
         {
             float New=(P_totalprice/100)*10;
             float total=P_totalprice-New;
             printf("%f\n",total); 

         }
        if(P_totalprice>=601)
        {
             float New=(P_totalprice/100)*15;
             float total=P_totalprice-New;
             printf("%f\n",total);    
        }  

                break;
            case 'a':
                return 1;
            default:
                ;
        }
        return 0;
    }

    void chech_out (struct Product *product)
    {
        printf("%2.f",product->potatoes);
    }

I get the error message:

hj4.c:181:28: runtime error: member access within null pointer of type 'struct Product'
hj4.c:181:28: runtime error: load of null pointer of type 'float'
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1476==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000422814 bp 0x7ffd19c2f280 sp 0x7ffd19c2f260 T1476)
==1476==The signal is caused by a READ memory access.
==1476==Hint: address points to the zero page.
    #0 0x422813  (/root/sandbox/hj4+0x422813)
    #1 0x42246e  (/root/sandbox/hj4+0x42246e)
    #2 0x42232a  (/root/sandbox/hj4+0x42232a)
    #3 0x7f11cd86fb96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #4 0x402ae9  (/root/sandbox/hj4+0x402ae9)

UndefinedBehaviorSanitizer can not provide additional info.
==1476==ABORTING

I guess I just need some pointers to solve this problem;)


Solution

  • I can't tell why your compiler is not warning about this, but your chech_out function is declared up top as chech_out() - with no parameters - but the definition includes one: chech_out(struct Product *product). When the function is called, it's treating garbage on the stack as a pointer to a product, and failing.

    Edit: the reason the compiler is not warning is because declaring functions up top with just open+close parentheses means the parameter list is undefined (to be compatible with older versions of C): if a function truly takes no parameters, defined them as int myfunction(void) where the void says "I take no parameters"

    To fix this

    // define struct Product here
    void get_number (void);
    void show_menu (void);
    int get_input (struct Product *);
    void chech_out (struct Product *);
    

    and now let your compiler tell you what you've missed.