So… I had to share this story, its so dumb.
I manage a plugin for some adobe software, it reads in a text file (that we generate in another app) then it processes the file, and creates all the objects inside adobe. Basically, takes their planning data and makes it real, so the artists can finalize it.
We have this bug, where not all the data would load. But it only happens on Mac, and only in CS6. Windows CS6, fine. Every other CS version, fine. Just Mac CS6.
I spent like multiple days just stepping through line by line (its like ~100k lines in the file we’re processing) trying to figure out WHY its failing .XCode is shitty for debugging, and since it only happens on mac, Im stuck using that. It cant even show me the values of the variables in the debugger properly. Ugh.
After like 3 days, I finally find out what is happening.
We have a function, called GetNextChar(). It gets the data stream (from a class Adobe provides and manages), reads in an int16 of data (2 bytes, since the data file is 2 byte encoding), then parses it to a character value and returns.
Theres a part where we look for <cr/> and replace it with a
, or something like that. So whenever we are reading the text, we use seek, to skip over those characters we don’t need. Works fine, convoluted maybe, but it works.
But I start noticing that the file stream was reporting “end of file” way too early.
I set up some debug data, namely where the stream pointer is at before and after the seek. Our seek goes back -10 bytes, so I set a check where if after the seek, our index is where we expect, and if its not, I just put a breakpoint.
So I run it again, and sure enough, I see this:
BeforeIndex: 500,000
After Index: 752,000
Da Fuck? Theres a bug in Adobes software (which they dont support and wont patch anymore), or also there might be something weird in the character data we are processing (the text file is run through customer scripts they’ve written themselves, so they might encode something weird? Dunno).
Basically, at some random point, the file stream will just fucking jump to the end. If I resave the file in my text editor, it doesn’t happen. But if I don’t, it does happen.
Well, fuck. How can I fix this, its a bug outside of my control…
then I get a crazy idea.
int beforeIndex = stream->GetPosition
Stream seek = stream->seek(seekVal*2, fromCurrentPosition);
int retval = stream->GetCharacter();
int afterIndex = stream->GetPosition
if(afterIndex != (beforeIndex + seekVal * 2)
{
//we've hit the random bug.
stream->seek(beforeIndex + (seekVal * 2), fromStartOfFile)
retVal = stream->GetCharacter();
}
return retVal
Surely, if I seek to the same spot, its just going to error out again right?
Nope. Nope.
It works.
So basically, now in a hacky move, when I detect the seek goes wrong, we instead of using seekFromCurrentPosition instead do SeekFromStartOfFile, and get the correct character.
And now the file loads properly.