Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void Q_onAssert() __attribute__ ((noreturn));
  5.  
  6. #define Q_ASSERT(test_) ((test_) ? (void)0 : Q_onAssert() )
  7.  
  8. void Q_onAssert() {
  9.     abort();
  10. }
  11.  
  12. typedef void (*VoidFunc)(void);
  13.  
  14. void sayHello()
  15. {
  16.     printf("Hello World\n");
  17. }
  18.  
  19. static VoidFunc getEventHandler()
  20. {
  21.     return sayHello;
  22. }
  23.  
  24. void indirectHello()
  25. {
  26.     VoidFunc handler = getEventHandler();
  27.     Q_ASSERT(handler != NULL);
  28.     handler();
  29. }
  30.  
  31. int main()
  32. {
  33.     indirectHello();
  34.     return 0;
  35. }
  36.  
  37. $ gcc temp.c; ./a.out
  38. Hello World
  39.  
  40.  
  41. $ cppcheck-1.90/cppcheck --enable=all temp.c
  42. Checking temp.c ...
  43. temp.c:28:5: warning: Either the condition 'handler!=NULL' is redundant or there is possible null pointer dereference: handler. [nullPointerRedundantCheck]
  44.     handler();
  45.     ^
  46. temp.c:27:5: note: Assuming that condition 'handler!=NULL' is not redundant
  47.     Q_ASSERT(handler != NULL);
  48.     ^
  49. temp.c:26:39: note: Assignment 'handler=getEventHandler()', assigned value is 0
  50.     VoidFunc handler = getEventHandler();
  51.                                       ^
  52. temp.c:28:5: note: Null pointer dereference
  53.     handler();
  54.     ^
  55. temp.c:14:0: style: The function 'sayHello' is never used. [unusedFunction]
  56.  
  57. ^
  58. nofile:0:0: information: Cppcheck cannot find all the include files (use --check-config for details) [missingIncludeSystem]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement