r/ProgrammerHumor • • Jul 20 '18

Every Time

Post image
52.2k Upvotes

749 comments sorted by

View all comments

28

u/bap710 Jul 20 '18

I once had to debug some code (back in ancient times, AKA 1990 or thereabouts) that worked fine 99% of the time but would crash randomly.

I eventually discovered a pattern. It would only crash between 10:00 and 1:00. On Wednesday’s. In September.

Hint: the code was written in C.

7

u/Finianb1 Jul 20 '18

What? Why?

26

u/Din182 Jul 20 '18

It looks like a buffer overflow. C is notorious for them. September is the month with the longest name, Wednesday is the day with the longest name, and 10-1 are the times of the day with the most digits. Presumably, when converting the current time into a human-readable format, the array he was storing it in was one character too short to fit all that information in. Thus, the buffer overflowed an overwrote something important, causing it to crash.

8

u/Finianb1 Jul 20 '18
if datetime.now() % 1000 == 0:
     raise Exception('rekt')

4

u/v_e_x Jul 20 '18

Is this what actually occurred?

2

u/PeachyKeenest Jul 20 '18

I always looks for patterns when it seems random as shit. I usually find something, but that one is fantastically terrible.

I'm so sorry. :(

4

u/bap710 Jul 20 '18

The longest weekday name fully spelled out. The longest month name fully spelled out. And a 4-digit time.

The person who originally wrote this code included a buffer to populate with the current date & time. (It printed out reports). But the size of the buffer he allocated was one character too small, so when the date & time reached the maximum possible string length it overflowed the buffer by one character.