UndefinedBehaviorSanitizer因为nullpointer的原因

问题描述 投票:0回答:2

Hej,我在C中有一些代码,可以完成很多工作。我只是想从Typedef土豆上获取价格,以方便结帐。我“ //”未使用的代码。我需要更多信息,请问,尽管我不知道如何解决此问题。因此,非常感谢您的帮助。

 #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);
    }

我收到错误消息:

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

我想我只需要一些指针即可解决此问题;)

c typedef null-pointer
2个回答
2
投票

我不知道为什么您的编译器没有对此发出警告,但是您的chech_out函数在顶部声明为chech_out()-没有参数-但definition包括一个:chech_out(struct Product *product)。调用该函数时,它会将堆栈上的垃圾视为指向产品的指针,然后失败。

编辑:编译器未发出警告的原因是因为在函数顶部仅加上了开闭括号,这意味着参数列表未定义(与C的较早版本兼容):如果函数确实不接受任何参数,则对其进行定义如int myfunction(void),其中void表示“我不带参数”

解决此问题

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

现在让您的编译器告诉您您错过了什么。


0
投票

谢谢,我尝试了Steve的代码,现在遇到了我不明白的编译问题。代码唯一的变化是:

void get_number (void);
void show_menu (void);
int get_input (struct Product *);
void chech_out (struct Product *);
int number;

我得到错误:

hj4.c:35:18: error: too few arguments to function call, expected 1, have 0
                if (get_input ()) // end on "a"
                    ~~~~~~~~~  ^
hj4.c:21:1: note: 'get_input' declared here
int get_input (struct Product *);
^
hj4.c:73:14: error: too few arguments to function call, expected 1, have 0
                        chech_out();
                        ~~~~~~~~~ ^
hj4.c:22:1: note: 'chech_out' declared here
void chech_out (struct Product *);
^
2 errors generated.

我确实尝试将代码更改为

void get_number (void);
void show_menu (void);
int get_input (struct Product *product);
void chech_out (struct Product *product);
int number;

但是出现了同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.