How a routine gem update ended up creating $73k worth of subscriptions
Posted by hartator@reddit | programming | View on Reddit | 4 comments
Posted by hartator@reddit | programming | View on Reddit | 4 comments
lisnter@reddit
Alignment is important. At one of my summer C programming jobs many years ago I wrote a parser that accepted and normalized incoming stock quote information from a number of different sources. One of the sources would consistently crash after some relatively short duration. It wasn't always the same incoming data but it always core dumped. The struct was something like:
struct quote {
int index ;
char *ticker ;
float ask ;
float bid ;
}
I started debugging and when putting in toy values for this string, I noticed that when the string length was a multiple of 4 it worked; all other lengths failed. Old-timers know where this is going.
I had a CPU alignment problem - totally my fault. I would malloc() a buffer for the incoming data like this:
buf = (char *)calloc( sizeof(int) + sizeof(float)*2 + strlen(ticker)+1 )
and jammed that into my struct * - don't you just love C! I do actually. . .
My problem, of course was that accessing the float after the char * would fail if it was not aligned on 32-bit boundaries. The environments were all Sun SPARC machines (the pizza-boxes if you remember that far back). Very fast (for the day) 32-bit machines.
Once I saw the behavior I knew what I had done and the fix was easy - just pad the string to the next 4-byte length. I was a very young programmer but this was a fun problem to debug and I learned a lot about hardware dependencies.
frzme@reddit
That story sounds good but at least with my understanding of C it doesn't add up:
Inside the struct you always have a pointer of a fixed size (32bit I guess, maybe 16?) how would changing the size of the buffer the pointer points to change anything?
I guess if you had a pointer to the struct which you manually position somewhere you could create a bug like this - but uncertain
F54280@reddit
No, it makes zero sense. He didn’t took into account the
char*
in his struct (which should not exist if he embeds the string at the end), so don’t waste your time.Edit: autocorrect typo
lisnter@reddit
Yeah. Sorry u/frzme and u/F54280 but I can assure you this was the problem. Perhaps I don't recall the details correctly (it was nearly 40 years ago) but these kinds of alignment problems happen when you're working with streamed data.
Also, smart-guy u/F54280 . . .grammar. . .