Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone
Visual studio adds guard bytes before and after local variables to detect buffer overruns, very usefull, but I want to disable it for some simple and short functions which are used so many times. for example, In this function
C++
int get_first_char( char * buf, size_t len )
{
  return len > 0 ? buf[0] : -1;
}
this function is only an example, please do not discuss it's usefullness.
I want visual studio in debug mode not to add guard bytes to stack when this function is called.
Is there any way to do this?

EDIT: building in release mode does not add guard bytes. I want to disable guard bytes for some simple functions only in debug mode, because testing my application in debug mode slows down significantly if that functions are called so many times.

thanks in advance
mr.abzadeh
Posted
Updated 18-Dec-12 13:26pm
v2

In VS 2010, open the project property pages, go to C/C++ -> Code Generation. There is an option called "Buffer Security Check" (/GS) which may be what you're talking about. You can disable it right there, or add "/GS-" on the command line to disable it.

P.S.: the help page on /GS offers additional information, among other things specific circumstances under which the compiler does not extend the buffers, e. g. when the first line of a function is inline assembly, or when it hs the keyword naked:

C++
__declspec( naked ) int func( formal_parameters ) {}

Check out naked in the help pages for more info.
 
Share this answer
 
v2
Comments
Pascal-78 19-Dec-12 11:34am    
"naked" will remove a lot of thing, like
the possibility to use C++ object with potential thrown exception (error C3068)
the possibility to initialize a variable at declaration (int a=1;) (error C2489)
may be _forceinline is a better choice.
mr.abzadeh 21-Dec-12 18:16pm    
/GS- does not remove guard bytes.
__declspec( naked ) does remove guard bytes, and It is a solution. but the epilog and prolog code for creating and removing stack frame will be left to programmer.
The only solution I could think of is to build it in Release mode. You can still debug it that way, but it will make some optimizations that Debug builds do not (for example, you may not always be able to hit break points on lines that declare variables, because the variable may be eliminated).
 
Share this answer
 
I tend to believe that the option is global if activated. That is it will apply to all memory allocations.

Are you doing a lot of memory allocation for performance to be a problem?

Also, are you sure that the penality is related to guard bytes ?
 
Share this answer
 
Comments
mr.abzadeh 19-Dec-12 6:15am    
Thanks for your reply.
I do not allocate a lot of memory, and the guard bytes added by malloc is not important for me. The guard bytes added to stack to protect local variable is important to me, and the performance penalti is because of it because I call that functions in deeper loops so many times. I can avoid calling that simple functions and write it's code in the calling function, but this affects readability of my code, and readablity is very important to me. In general, I think there is no need to insert guard bytes to stack if a function does not use local variables, does not call functions and does not modify it's arguments.
mr.abzadeh 19-Dec-12 6:24am    
Surely I do not want to disable guard bytes globally, but I wish to know how to disable them globally, If possible.
mr.abzadeh
May be you can use the pragma directive check_stack to turn off stack probes.
C++
#pragma check_stack(off)
int get_first_char( char * buf, size_t len )
{
  return len > 0 ? buf[0] : -1;
}
 
Share this answer
 
Comments
mr.abzadeh 21-Dec-12 18:09pm    
I examined check_stack pragma, and it does not affect guard bytes
Pascal-78 22-Dec-12 12:48pm    
After a search on the net, it seems this pragma is useless in debug-mode because overridden by a compiler option.
I tried modifying some compilers options and it seems that your problem can be solved with removing the /RTCs, /RTC1 (/RTCs + /RTCu) this option is available in "C/C++ - Code Generation" properties of your projet.
mr.abzadeh 22-Dec-12 16:14pm    
As I understood, /RTCu and /RTCs generate extra code to capture bugs such as using uninitialized variables, detection of buffer underrun and overrund, and so on. What I need is to remove guard bytes only in a few functions (not module) in debug mode, and I Think the only solution is
declaring these few functions as __declspec(naked), which removes guard bytes completely, but need some low level programming, as creating and removing stack frame( allocating and destroying local variables ) is left to the programmer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900