string operation and related exception

This is a discussion on string operation and related exception within the C++ Programming forums, part of the General Programming Boards category; Hi Daved, In release mode, this line will check the boundary. Any comments? Code: _SCL_SECURE_VALIDATE_RANGE(_Pos < size()); Originally Posted by ...

  1. #31
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Daved,


    In release mode, this line will check the boundary. Any comments?

    Code:
    _SCL_SECURE_VALIDATE_RANGE(_Pos < size());
    Quote Originally Posted by Daved View Post
    The release version only has range checking if the _HAS_ITERATOR_DEBUGGING symbol is defined and set to 1. My guess is that by default it is not, so by default the release version skips the range check.

    So what you're seeing is that the code follows what CornedBee stated, and additionally implemented Elysia's preference that there be a switch of some kind to allow boundary checking even in release build.

    regards,
    George

  2. #32
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Can you post the definition of _SCL_SECURE_VALIDATE_RANGE?

  3. #33
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sure, Daved.


    Here it is. Any comments?

    Code:
     #define _SCL_SECURE_VALIDATE_RANGE(cond)				\
    	{													\
    		if (!(cond))									\
    		{												\
    			_ASSERTE((#cond, 0));						\
    			_SCL_SECURE_OUT_OF_RANGE_NO_ASSERT;			\
    		}												\
    		__analysis_assume(cond);						\
    	}
    Quote Originally Posted by Daved View Post
    Can you post the definition of _SCL_SECURE_VALIDATE_RANGE?

    regards,
    George

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Here it is. Any comments?
    Question: Is it always defined like that, or is its definition dependent on some other symbol in the way that _HAS_ITERATOR_DEBUGGING is?

  5. #35
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    I think always, Daved. If I am not correct, please feel free to correct me. Any comments?

    Quote Originally Posted by Daved View Post
    >> Here it is. Any comments?
    Question: Is it always defined like that, or is its definition dependent on some other symbol in the way that _HAS_ITERATOR_DEBUGGING is?

    regards,
    George

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    On closer inspection, if _SECURE_SCL is defined to 1, _SCL_SECURE_VALIDATE_RANGE is defined as George2 has posted. Otherwise it is simply defined as _SCL_SECURE_VALIDATE_RANGE.
    Nothing, in other words.
    So far as I see, _SECURE_SCL is not dependant on Release/Debug and defaults to 1 if not defined. It may be defined in other headers, however. Not sure.

    In Debug, operator [] throws a fit several times (ASSERTs), does not throw.
    at() throws in both debug and release.
    if _SECURE_SCL is not defined, then release ignores invalid subscript ranges (go figure) (release).
    Debug still complains about out of range.
    And lastly, iterator debugging is defined in both release/debug and _SECURE_SCL is always defined to one inside vector header.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #37
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Elysia,


    Three more comments,

    1. What is your conclusion? operator[] can throw in release mode?

    2. I do not know quite understand what do you mean in the following sentence,

    --------------------
    In Debug, operator [] throws a fit several times (ASSERTs), does not throw.
    --------------------

    3. How _SECURE_SCL is defined or not? Is it implicitly defined when some other commonly used macros are defined?

    You mentioned both throws and not throw. Sorry English is not my native language, what do yo mean? :-)

    Quote Originally Posted by Elysia View Post
    On closer inspection, if _SECURE_SCL is defined to 1, _SCL_SECURE_VALIDATE_RANGE is defined as George2 has posted. Otherwise it is simply defined as _SCL_SECURE_VALIDATE_RANGE.
    Nothing, in other words.
    So far as I see, _SECURE_SCL is not dependant on Release/Debug and defaults to 1 if not defined. It may be defined in other headers, however. Not sure.

    In Debug, operator [] throws a fit several times (ASSERTs), does not throw.
    at() throws in both debug and release.
    if _SECURE_SCL is not defined, then release ignores invalid subscript ranges (go figure) (release).
    Debug still complains about out of range.
    And lastly, iterator debugging is defined in both release/debug and _SECURE_SCL is always defined to one inside vector header.

    regards,
    George

  8. #38
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    It still seems as if you can govern whether bounds checking is done with the _SECURE_SCL symbol.

    Although given Microsoft's recent attention to security and avoiding things like buffer overruns, it wouldn't be that surprising if it now defaulted to range checking in release build.

    Have you looked at the Microsoft documentation? That might have better information than trying to read though the code itself.

  9. #39
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by Elysia View Post
    I think either I would allow a flag to be set (EnableExceptions) or differentiate two types (perhaps just different names for initializing a constructor in different ways.
    A flag sucks because somebody has to set it, and you never know which way it was set by the person before you.

    vector(bool bEnableExceptions = false)
    So now I have to specify a fundamental design decision (whether the operator[] does bounds checks) every time I declare a vector?

    #define SafeVector vector(true)
    I literally barfed on that one..

    I would expect the operator [] and the member function at both to throw exceptions if exceptions are enabled. Otherwise if they aren't enabled, none should throw (or make out of bounds checks).
    I expect cars to have seven cylinders, and be built entirely of fiberglass.

    I understand they want to keep C compability, but then you would be using a C array and not std::vector since that wouldn't even compile under C so not C project would ever use std::vector.
    Why would I want to give up all the conveniences of a std::vector and go back to dumb arrays just because I don't want bounds checking?

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by Daved View Post
    It still seems as if you can govern whether bounds checking is done with the _SECURE_SCL symbol.
    Correct.

    Quote Originally Posted by Daved View Post
    Although given Microsoft's recent attention to security and avoiding things like buffer overruns, it wouldn't be that surprising if it now defaulted to range checking in release build.
    Also correct. Bounds checking is done in release, as well. It doesn't throw, but it ASSERTs, unless _SECURE_SCL is defined to 0.

    Quote Originally Posted by Daved View Post
    Have you looked at the Microsoft documentation? That might have better information than trying to read though the code itself.
    Yep, there's a whole topic on the subject. I have to study it and divulged all information first before reporting back.

    Quote Originally Posted by George2 View Post
    1. What is your conclusion? operator[] can throw in release mode?
    No. Never. It can only raise an assert.

    Quote Originally Posted by George2 View Post
    2. I do not know quite understand what do you mean in the following sentence,
    In debug, operator [] throws several asserts, one after another.
    In release, only one assert is raised.

    Quote Originally Posted by George2 View Post
    3. How _SECURE_SCL is defined or not? Is it implicitly defined when some other commonly used macros are defined?
    It's defined inside the vector headers as I can see. If not defined previously, it's defined to 1 by default, meaning enabled.
    You can still define it to 0 before including the header and it will remain 0.

    Quote Originally Posted by George2 View Post
    You mentioned both throws and not throw. Sorry English is not my native language, what do yo mean? :-)
    Throw = function can/will throw (for example, member function at(), both in debug and release).
    Not throw = the function will not throw (operator [] for example, it will assert but never throw in either debug or release).

    Quote Originally Posted by brewbuck View Post
    Why would I want to give up all the conveniences of a std::vector and go back to dumb arrays just because I don't want bounds checking?
    And that's why there should be the option of doing bounds checking or not. There could even be a function that says if bounds checking is on or off.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #41
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    1.

    Quote Originally Posted by Elysia View Post
    Yep, there's a whole topic on the subject. I have to study it and divulged all information first before reporting back.
    Could you provide an URL please?

    2.

    I am wondering how assert is implemented internally? Using some soft interrupt or through by exception handling approach?


    regards,
    George

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by George2 View Post
    Could you provide an URL please?
    Don't know about URLs since I have MSDN installed locally. Anyway, the topic subject is "Checked Iterators". So search for that on MSDN.

    2. I am wondering how assert is implemented internally? Using some soft interrupt or through by exception handling approach?
    They use a hardware interrupt, __asm int 3 on Intel & AMD processors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by George2 View Post
    I am wondering how assert is implemented internally? Using some soft interrupt or through by exception handling approach?
    something like
    Code:
    #undef  assert
    
    #ifdef  NDEBUG
    
    #define assert(exp)     ((void)0)
    
    #else
    
    #ifdef  __cplusplus
    extern "C" {
    #endif
    
    _CRTIMP void __cdecl _assert(void *, void *, unsigned);
    
    #ifdef  __cplusplus
    }
    #endif
    
    #define assert(exp) (void)( (exp) || (_assert(#exp, __FILE__, __LINE__), 0) )
    
    #endif  /* NDEBUG */
    <removed copyrighted code, sorry - CornedBee>

    So it is just an __crtMessageBoxA call followed by _DbgBreak to start debugging
    Last edited by CornedBee; 03-02-2008 at 07:04 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Here's another interesting tidbit.
    There's another interesting define called _SECURE_SCL_THROWS.
    If defined to 1, operator [] will throw an exception is it's out of range.
    It also requires _SECURE_SCL to be defined as 1.

    Microsoft also provides special functions in the library. They provide checked integrators (access will be checked) and non-checked iterators (access will not be checked).
    If you've defined _SECURE_SCL to 1 (meaning you want checked access), all accesses will be checked. If you call functions that do not normally perform a check, you'll get a warning and still get checked access.
    However, if it's defined to 0 (meaning you don't want checked access), you won't get any access check unless you you a checked iterator (checked_iterator), which is a Microsoft extension.
    Last edited by Elysia; 03-02-2008 at 06:15 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #45
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Quote Originally Posted by Elysia View Post
    They use a hardware interrupt, __asm int 3 on Intel & AMD processors.
    I know int 3 will make a H/W interrupt. And then invoke the interruption handler for int 3. I am wondering how does the assert dialog opens? Visual Studio registers or Windows registers the interrupt handler, and the interrupt handler's task is to display the dialog and some debug information?


    regards,
    George

Page 3 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Looking for examples for string related programs
    By koloth in forum C Programming
    Replies: 5
    Last Post: 04-15-2003, 12:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21