views:

51

answers:

2

I was once asked during an interview the following question, and I've still never been quite clear on the answer. I was wondering if anyone knew where I could learn more, googling hasn't been much help:

Say you have a program that you'd like to test. You add a single log statement and suddenly, the program which was producing expected output stops producing expected output. What could have happened?

+3  A: 

Aha. I've actually had this happen.

Let's consider a program that has a bug that is munging the stack. If you introduce a log or print statement, the call to the log may shift the stack enough to cause it to change behaviour.

It's an interesting problem to think how to show an example. Probably easiest to do it with a bad format in a printf...

okay, in outline at least an example will look like this.

int parent(){ ... printf("%s\n", itoa(child()));

int child(){
    int num;
    scanf("%d%d", num);  /* notice the format; scanf is going to eat more of the
                           * stack than it should.
                           */
    return num;            /* but this return may unwind the stack successfully. */
}

Your case would happen if you insert a printf() just before the return.

Charlie Martin
"minging the stack"? Ming is not a verb and if it were, in this context I don't see what any of it has to do with the Ming Dynasty in China.
Colin Mackay
+1: I've seen this too, and it's horrible to debug!
Michael van der Westhuizen
@Colin Minging is British slang for ugly, so I read this as uglifying :-)
Michael van der Westhuizen
Ratnesh Maurya
Sadly, Michael, it was a good attempt at a save that I just ruined by correcting my own typo.I'm trying to get somewhere with "Minging the merciless" but it's too late at night....
Charlie Martin
Definitions of munging on the Web: * Mung is computer jargon for "to make repeated changes which individually may be reversible, yet which ultimately result in an unintentional ... en.wikipedia.org/wiki/Munging * Munge - In computing, the term munge [mʌndʒ] means to create a strong, secure password through character substitution. "Munge" is sometimes backronymmed as Modify Until Not Guessed Easily. ... en.wikipedia.org/wiki/Munge
Boris Pavlović
I appreciate the solution, but I'm afraid I don't quite follow what will happen. I don't know what to expect of the scanf call specifying two numbers but providing only one. Could you walk me through more of the detail? Thanks!
realmlord44
+1  A: 

Your program might have race conditions between concurrent threads, so any change to the timing may change the program behaviour.

Usually this is the other way round, which is much worse (the so called Heisenbug): Your program misbehaves and you want to debug it by adding log output. But the log output makes the problem go away, so it becomes very hard to diagnose.

starblue