c++ - How to report a stack buffer overrun on Windows? -


in code shown below have used documented ways detect exception , produce diagnostic. uses c++ try/catch keywords, catches seh exception __try/__catch extension keywords, uses windows' addvectoredexceptionhandler() , setunhandledexceptionfilter() winapi functions install veh/seh filters.

running visual c++ 2003:
/gs: outputs "hello,world!" , terminates exit code 0.
/gs-: outputs "hello,world!" , terminates exit code 0.

running visual c++ 2013:
/gs: no output, terminates exit code -1073740791
/gs-: outputs "hello,world!" , terminates exit 0.

how produce diagnostic in vs2013 compiled program /gs in effect?

#include "stdafx.h" #include <windows.h>  #define call_first 1   #define call_last 0  long winapi myvectoredhandler(struct _exception_pointers *exceptioninfo) {     unreferenced_parameter(exceptioninfo);      printf("myvectoredhandler\n");     return exception_continue_search; }  long winapi myunhandledexceptionfilter(_in_ struct _exception_pointers *exceptioninfo) {     printf("setunhandledexceptionfilter\n");      return exception_continue_search; }  void f() {     __try     {         char p[20] = "hello,world!";         p[24] = '!';         printf("%s\n", p);     }     __except (exception_execute_handler)     {         printf("f() exception\n");     } }  int _tmain(int argc, _tchar* argv[]) {     addvectoredexceptionhandler(call_first, myvectoredhandler);     setunhandledexceptionfilter(myunhandledexceptionfilter);      try{         f();     }     catch (...){         printf("catched f exception\n");     }     return 0; } 

there no solution question asked.

overrunning array causes undefined behaviour in standard c++, no particular result guaranteed. failure give reliable result not problem compiler - permitted behaviour.

i'm aware of no implementation guarantees specific behaviour in response overrun - vs doesn't. hardly surprising compilers not required (that is, essentially, meaning of undefined behaviour). reason case is difficult reliably or consistently detect such occurrences.

this means consistent way detect array overrun check array indices valid before using them access array element , take appropriate actions (e.g. throw exception can caught instead of doing bad operation). downside not provide simple or reliable way catch errors in arbitrary code - short of modifying code required checks.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -