I am a big fan of GCC, but recently I noticed a foggy anomaly. Using __gnu_cxx :: __ normal_iterator (i.e., the most common type of iterator used in libstdC ++, C ++ STL), you can access an arbitrary memory cell and even change its value without causing an exception! Is this expected behavior? If so, is this not a security loophole?
Here is an example:
#include <iostream> using namespace std; int main() { basic_string<char> str("Hello world!"); basic_string<char>::iterator iter = str.end(); iter += str.capacity() + 99999; *iter = 'x'; cout << "Value: " << *iter << endl; }
Declaring an iterator outside the container from which it was obtained is undefined behavior, and doing nothing is just an opportunity.
, , , , . MSVS ( , , =. .
, Dinkumware (STL VS) ( , ), , , . .
, . , :
for ( type::const_iterator it = obj.begin(); it != obj.end(); ++it ){ // Refer to element using (*it) }
end(). , , , < > end(). C ++ , Java, , , .
end()
++ , . , . :
if (iter < str.begin() || iter >= str.end()) throw something;
. . , .
$ ./a.exe 11754 [main] a 4992 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack) Segmentation fault (core dumped)
Undefined behavior can mean different things on different compilers, platforms, days. Perhaps when you started it, the address created by all this addition accidentally fell into another valid memory space. Maybe you, for example, added from the stack to the heap.
Source: https://habr.com/ru/post/1739617/More articles:What is the difference between these usages: the new SubElement () and SubElement ()? - c ++How to find a word in a Mac OS X dictionary using AppleScript? - dictionaryUnit testing application 'legacy' WPF - c #Custom Links in RichTextBox - c #Why does the minus operator give a different result than the TIMESTAMPDIFF () function in mysql? - mathQuery the same search table with multiple columns - sqlAny way to import all cases from one FogBugz account to another (both are hosted) - fogbugz-on-demandHow can I make the width of a dynamic page to a certain limit? - javascriptSaving variables in memory, C ++ - cSorting vector of custom objects - javaAll Articles