bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

couple of simple bugs in hanoi.c example


From: Oliver Schonrock
Subject: couple of simple bugs in hanoi.c example
Date: Sun, 20 Oct 2019 23:09:48 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0

Thanks for ncurses!

While playing with hanoi.c example I found a couple of bugs. Patch at
bottom.

The first 2 need the bounds check before accessing the memory location
(valgrind complains). Short circuit eval.

The second 2 are just wrong. not 0=>n_discs but 0=>2 ! ie 3 pegs with
n_discs slots on them, not n_discs pegs! This was causing a segfault on
program exit on my machine for >3 discs.

Valgrind seems all happy now.

Hope that helps

@@ -79,13 +79,13 @@

        --p_my_pegs[src].n_discs;
        index = 0;
-       while(p_my_pegs[src].sizes[index] == 0 && index != n_discs)
+       while(index != n_discs && p_my_pegs[src].sizes[index] == 0)
                ++index;
        temp = p_my_pegs[src].sizes[index];
        p_my_pegs[src].sizes[index] = 0;

        index = 0;
-       while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs)
+       while(index != n_discs && p_my_pegs[dst].sizes[index] == 0)
                ++index;
        --index;
        p_my_pegs[dst].sizes[index] = temp;
@@ -100,7 +100,7 @@
        /* Allocate memory for size array
         * atmost the number of discs on a peg can be n_discs
         */
-       for(i = 0; i < n_discs; ++i)
+       for(i = 0; i < 3; ++i)
                p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int));
        size = 3;
        for(i = 0;i < n_discs; ++i, size += 2)
@@ -148,7 +148,7 @@
 void free_pegs(peg *p_my_pegs, int n_discs)
 {      int i;

-       for(i = 0;i < n_discs; ++i)
+       for(i = 0;i < 3; ++i)
                free(p_my_pegs[i].sizes);
 }

-- 
Oliver Schönrock
Mobile   : +44 7880 617 446
email    : address@hidden

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]