J
jl_post
Dear C++ community,
You know how sometimes an action that can cause a crash doesn't
always crash, but can cause undefined behavior later in the program?
Well, my question is: If an undefined read-only operation somehow
avoids crashing, can it still cause a crash later in unrelated code?
Allow me to illustrate with a pseudo-program:
#include <iostream>
#include <vector>
int main(int argc, char ** argv)
{
// Part A: Find the biggest element of an array.
{
std::vector<unsigned int> array; // empty array!
unsigned int biggestElement = 0;
for (unsigned int i = 0; i < 100; i++)
{
// Since array is empty, the following lines are
// undefined behavior, and could cause a crash:
if (array > biggestElement)
biggestElement = array;
}
std::cout << "Biggest element is: "
<< biggestElement << std::endl;
}
// Part B: Do everything else.
std::cout << "Hello, world!" << std::endl;
// (Lots of code goes here.)
return 0;
}
In this program, the for-loop in Part A iterates 100 times over an
empty array. Many programmers would expect this to crash, but as with
many undefined behaviors, a crash is not guaranteed to happen; the
program could conceivably "survive" and make it into Part B.
Notice that Parts A and B are independent: nothing is carried over
from Part A that can affect the logic of Part B. Knowing this fact,
if the program does indeed make it to Part B, could Part B still crash
(or produce undefined behavior) simply because of the array-bounds
read error in Part A?
In other words, could the read-only error in Part A conceivably
corrupt memory (and cause a crash) for Part B, even though they share
independent logic?
(I'm asking this question because I want to know if undefined read-
only behavior (that does not corrupt the program logic) can corrupt
memory that can cause further problems later in the program's
execution.)
Thanks,
-- Jean-Luc
You know how sometimes an action that can cause a crash doesn't
always crash, but can cause undefined behavior later in the program?
Well, my question is: If an undefined read-only operation somehow
avoids crashing, can it still cause a crash later in unrelated code?
Allow me to illustrate with a pseudo-program:
#include <iostream>
#include <vector>
int main(int argc, char ** argv)
{
// Part A: Find the biggest element of an array.
{
std::vector<unsigned int> array; // empty array!
unsigned int biggestElement = 0;
for (unsigned int i = 0; i < 100; i++)
{
// Since array is empty, the following lines are
// undefined behavior, and could cause a crash:
if (array > biggestElement)
biggestElement = array;
}
std::cout << "Biggest element is: "
<< biggestElement << std::endl;
}
// Part B: Do everything else.
std::cout << "Hello, world!" << std::endl;
// (Lots of code goes here.)
return 0;
}
In this program, the for-loop in Part A iterates 100 times over an
empty array. Many programmers would expect this to crash, but as with
many undefined behaviors, a crash is not guaranteed to happen; the
program could conceivably "survive" and make it into Part B.
Notice that Parts A and B are independent: nothing is carried over
from Part A that can affect the logic of Part B. Knowing this fact,
if the program does indeed make it to Part B, could Part B still crash
(or produce undefined behavior) simply because of the array-bounds
read error in Part A?
In other words, could the read-only error in Part A conceivably
corrupt memory (and cause a crash) for Part B, even though they share
independent logic?
(I'm asking this question because I want to know if undefined read-
only behavior (that does not corrupt the program logic) can corrupt
memory that can cause further problems later in the program's
execution.)
Thanks,
-- Jean-Luc