Lena inherited some C++ code which had issues regarding a timeout. While skimming through the code, one block in particular leapt out. This was production code which had been running in this state for some time.
if((pFile) && (pFile != (FILE *)(0xcdcdcdcd))) {
fclose(pFile);
pFile = NULL;
}
The purpose of this code is, as you might gather from the call to fclose
, to close a file handle represented by pFile
, a pointer to the handle. This code mostly is fine, but with one, big, glaring “hunh?” and it’s this bit here: (pFile != (FILE *)(0xcdcdcdcd))
(FILE *)(0xcdcdcdcd)
casts the number 0xcdcdcdcd
to a file pointer- essentially it creates a pointer pointing at memory address 0xcdcdcdcd. If pFile
points to that address, we won’t close pFile
. Is there a reason for this? Not that Lena could determine from the code. Did the 0xcdcdcdcd
come from anywhere specific? Probably a previous developer trying to track down a bug and dumping addresses from the debugger. How did it get into production code? How long had it been there? It was impossible to tell. It was also impossible to tell if it was secretly doing something important, so Lena made a note to dig into it later, but focused on solving the timeout bug which had started this endeavor.