2

Can someone gives me an example of deterministic bug in a program?

Thanks.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Lok
  • 21
  • 1
  • Deterministic bugs are easy -- they always happen! It's the non-deterministic ones that are hard (race conditions, and such). – Gabe Jan 21 '11 at 05:43

2 Answers2

2
void bug()
{
  int *ptr = 0;
  ptr[0] = 10;
}

Accessing a null pointer. Always happens.

Nick Banks
  • 4,298
  • 5
  • 39
  • 65
  • Note however that the result of accessing a null pointer may vary, depending on what system the program is running on... – Jeremy Friesner Jan 21 '11 at 05:45
  • That's not deterministic since the behaviour is undefined. That means it may do absolutely nothing on one run while collapsing the universe to a singularity on the next. – paxdiablo Jan 21 '11 at 05:52
  • I am looking for example of using assignment variable. – Lok Jan 21 '11 at 05:53
  • @Lok Can you elaborate on that? – Nick Banks Jan 21 '11 at 05:55
  • @paxdiablo Show me a system that attempting to set the data at address 0 won't cause a crash. Every time. – Nick Banks Jan 21 '11 at 05:56
  • I am looking for a simple one sentence to describe the deterministic bug (e.g assignment variable x=2 instead of x=4). But im not sure does it make sense. – Lok Jan 21 '11 at 05:59
  • HP-UX 9 (the last version I used, I have no idea what it does now) allowed reads and writes to memory location 0. You had to actually turn on an option to catch this. But, to be honest, I don't need to - the standard says it's undefined which means it may work perfectly :-) – paxdiablo Jan 21 '11 at 06:03
  • @gamernb Okay: AmigaDOS. Writing to address 0 might give you an Enforcer hit (if you are running Enforcer) but other than that it wouldn't crash the app. (No memory protection in AmigaDOS... an OS where men were men, and writing to incorrect memory locations would often crash the whole machine, and if you were lucky, corrupt the file system too ;)) – Jeremy Friesner Jan 26 '11 at 00:27
1

Here's a deterministic bug:

void myStrCpy (char *s, char *d) {
    for (int i = 0; i < strlen (s) - 1; i++)
        *d++ = s[i];
    *d = '\0';
}

It very obviously forgets to copy the final character of the string meaning the copy is one character less.

For one with an assignment statement, you could use:

#include <stdio.h>
int main (void) {
    short x = 2094967295;
    printf ("%d\n", x);
    return 0;
}

which outputs -22017 consistently.

Or even a little snippet from a recent question on SO:

int x = 2^5;   // set x to 32 for bitmasking later.

That's a bug, albeit a user error one.

Or even:

int *x = malloc (4);

in an application that's supposed to be portable.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953