Thread: Really Difficult Debug

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    Exclamation Really Difficult Debug

    This is my first time here. Hello everyone

    I had this posted over at dreamincode.net for about 4 days now. None over there can help me.
    So i came here.

    Intent:
    This code is intended to be used latter in an encryption program.

    Notices:
    I have provided a BackTrace.
    Also, when i remove the function call seed_gen() the next function
    in line is identified as the problem. This is od,, i have looked at my
    code in detail, i can find no problem (and neither can anyone at </D.I.C>).
    Where i am seeding srand() with 3.141, i intedn to use time() in the final release.

    Code / Errors / Debugg:

    Main.cpp:
    Code:
    #include <cstdlib>
    #include <cstdio>
    #include <ctime>
    #include <iostream>
    #include <fstream>
    #include "cipher.h"
    
    using namespace std;
    
    cipher Ciph1;
    
    int main(){
         Ciph1.seed_gen();
         Ciph1.ciph_gen();
         if(Ciph1.cipher_any() != true){
             cout << "Cipher out of range.\n";
             cin.get();
             return 0;
         }
         Ciph1.ciph_pri();
         cin.get();
    }
    Cipher.h:
    Code:
    #ifndef CIPHER_H
    #define CIPHER_H
    
    /** _--+++Written by Delta+++--_ **/
    /** Editing and advice:
        dreaminocde.net members:
        polymath
        cerolobo
        AmitTheInfinity
    **/
    
    #include <stdio.h>
    #include <cstdlib>
    #include <time.h>
    #include <iostream>
    
    using namespace std;
    
    class cipher{
        public:
        // Public Variables
        int MyCipher[512][10];
        int MySeed[512];
    
        // Public Functions
    
         // ***Seed Generator***
         void seed_gen(){
             srand(time(NULL)); //Only seed random once in a function
             for (int a = 0;a < 511;a++){
             MySeed[a] = (rand() %10);
             }
         }
    
         // ***Cipher Generator***
         // ciph_gen();
         void ciph_gen(){
             // Stage 1: Set each first element of
             // each line to the seed value of that
             // line
             for (int a = 0;a < 512;a++){
                 MyCipher[a][1] = MySeed[a];
             }
                 // Stage 2
                 for (int b = 0;b < 511;b++){
                     for (int c = 0;c < 9;c++){
                        if (MyCipher[b][c--] == 9){
                            MyCipher[b][c--] = 0;
                        }
                        MyCipher[b][c] = (MyCipher[b][c--]) +1;
                     }
                 }
         }
    
    
    
    
    
         // ***Cipher Anylizer***
         // ciph_any();
         bool cipher_any(){
             for (int a = 0;a < 512;a++){
                 for(int b = 0;b < 10;a++){
                     if (MyCipher[a][b] > 9 || MyCipher[a][b] < 0){
                         return false; // Integrity = VOID
                     }
                 }
             }
         }
    
         // ***Print Cipher***
         // ciph_pri();
         void ciph_pri(){
             for (int a = 0;a < 512;a++){
                 for (int b = 1;b < 10;b++){
                     cout << MyCipher[a][b];
                 }
                 cout << "\n";
             }
         cin.get();
         }
    
    };
    #endif
    VALGRIND and GDB:
    Code:
    [Delta@localhost ~]$ cd Crypt
    [Delta@localhost Crypt]$ g++ main.cpp -o build1 -g
    [Delta@localhost Crypt]$ ./build1
    Segmentation fault
    [Delta@localhost Crypt]$ gdb build1
    GNU gdb Fedora (6.8-11.fc9)
    Copyright (C) 2008 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i386-redhat-linux-gnu"...
    (gdb) start
    Breakpoint 1 at 0x8048751: file main.cpp, line 13.
    Starting program: /home/Delta/Crypt/build1 
    main () at main.cpp:13
    13	     Ciph1.seed_gen();
    (gdb) bt
    #0  main () at main.cpp:13
    (gdb) list
    8	using namespace std;
    9	
    10	cipher Ciph1;
    11	
    12	int main(){
    13	     Ciph1.seed_gen();
    14	     Ciph1.ciph_gen();
    15	     if(Ciph1.cipher_any() != true){
    16	         cout << "Cipher out of range.\n";
    17	         cin.get();
    (gdb) print Ciph1
    $1 = {MyCipher = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0} <repeats 512 times>}, 
      MySeed = {0 <repeats 512 times>}}
    (gdb) quit
    The program is running.  Exit anyway? (y or n) y
    [Delta@localhost Crypt]$ valgrind ./build1
    ==3243== Memcheck, a memory error detector.
    ==3243== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
    ==3243== Using LibVEX rev 1804, a library for dynamic binary translation.
    ==3243== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
    ==3243== Using valgrind-3.3.0, a dynamic binary instrumentation framework.
    ==3243== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
    ==3243== For more details, rerun with: -v
    ==3243== 
    ==3243== Invalid free() / delete / delete[]
    ==3243==    at 0x40052EA: operator delete(void*, std::nothrow_t const&) (vg_replace_malloc.c:354)
    ==3243==    by 0x42AE118: std::__verify_grouping(char const*, unsigned, std::string const&) (locale_facets.cc:108)
    ==3243==    by 0x42AF04C: std::locale::_Impl::_Impl(char const*, unsigned) (localename.cc:218)
    ==3243==    by 0x42AF0CC: std::locale::_Impl::_Impl(char const*, unsigned) (localename.cc:206)
    ==3243==    by 0x42B01F7: std::locale::locale() (basic_string.h:2189)
    ==3243==    by 0x42AB1CC: std::locale::_Impl::_Impl(std::locale::_Impl const&, unsigned) (locale.cc:249)
    ==3243==    by 0x8048704: __static_initialization_and_destruction_0(int, int) (iostream:77)
    ==3243==    by 0x804873D: _GLOBAL__I_Ciph1 (main.cpp:22)
    ==3243==    by 0x8048ADC: (within /home/Delta/Crypt/build1)
    ==3243==    by 0x8048557: (within /home/Delta/Crypt/build1)
    ==3243==    by 0x8048A78: __libc_csu_init (in /home/Delta/Crypt/build1)
    ==3243==    by 0x5AD570: (below main) (libc-start.c:179)
    ==3243==  Address 0x4347188 is not stack'd, malloc'd or (recently) free'd
    ==3243== 
    ==3243== Process terminating with default action of signal 11 (SIGSEGV)
    ==3243==  Bad permissions for mapped region at address 0x8048FFC
    ==3243==    at 0x8048899: cipher::ciph_gen() (cipher.h:50)
    ==3243==    by 0x8048768: main (main.cpp:14)
    ==3243== 
    ==3243== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 15 from 1)
    ==3243== malloc/free: in use at exit: 0 bytes in 0 blocks.
    ==3243== malloc/free: 0 allocs, 1 frees, 0 bytes allocated.
    ==3243== For counts of detected errors, rerun with: -v
    ==3243== All heap blocks were freed -- no leaks are possible.
    Segmentation fault
    [Delta@localhost Crypt]$
    Any help would be greatly appreciated.
    Thanks

    Live long and code strong.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >None over there can help me.
    That's interesting, because the bug isn't hard to spot and it's easily confirmed by stepping through the code. You're totally blowing away memory in cipher::ciph_gen because c becomes negative almost immediately. Your condition for that inner loop is c < 9, so it runs until c happens to wrap around or overflow (trashing memory the whole time), which I doubt is what you intended.
    My best code is written with the delete key.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Hard to tell.

    One thing is that you are quite relaxed with magic values for loop conditions: sometimes they go up to 512, sometimes up to 511 etc.

    Code:
    for (int c = 0;c < 9;c++){
        if (MyCipher[b][c--] == 9){
            MyCipher[b][c--] = 0;
        }
        MyCipher[b][c] = (MyCipher[b][c--]) +1;
    }
    It looks like this would cause out of bounds access (c can get smaller than 0) and in the last line where you both decrement and look-up the value c twice, the value of c is undefined: you can't rely that side-effects are applied as you read the line from left to right.

    I would recommend avoiding magic values and mixing increment/decrement operators into complex statements.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1
    Code:
    class cipher{
        public:
        // Public Variables
        int MyCipher[512][10];
        int MySeed[512];
    
        // Public Functions
    
         // ***Seed Generator***
         void seed_gen(){
             srand(time(NULL)); //Only seed random once in a function
             for (int a = 0;a < 511;a++){
             MySeed[a] = (rand() %10);
             }
         }
    MySeed[511] (the last element of the array) never gets initialized since the loop stops to soon.

    #2
    Code:
    // Stage 1: Set each first element of each line to the seed value of that line
    for (int a = 0;a < 512;a++){
        MyCipher[a][1] = MySeed[a];
    }
    The "first" element of any array is the element at the 0th index, not the 1st index (which is the second element of the array).

    #3
    Code:
    for (int b = 0;b < 511;b++){
        for (int c = 0;c < 9;c++){
            if (MyCipher[b][c--] == 9){
                MyCipher[b][c--] = 0;
            }
            MyCipher[b][c] = (MyCipher[b][c--]) +1;
        }
    }
    Again, one of your loops stop to short. Also, your use of c-- is worrying. The first time through the inner loop, c is 0. After the "if" test, c is decremented becoming a negative value. Then, you use that negative index later on in the next couple lines (while decrementing c even further in the process).

    #4
    Code:
    bool cipher_any(){
        for (int a = 0;a < 512;a++){
            for(int b = 0;b < 10;a++){
                if (MyCipher[a][b] > 9 || MyCipher[a][b] < 0){
                    return false; // Integrity = VOID
                }
            }
        }
    }
    This function does not return any true value at the end like I would expect.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    I believe your problem lies here:
    Code:
                 // Stage 2
                 for (int b = 0;b < 511;b++){
                     for (int c = 0;c < 9;c++){
                        if (MyCipher[b][c--] == 9){
                            MyCipher[b][c--] = 0;
                        }
                        MyCipher[b][c] = (MyCipher[b][c--]) +1;
                     }
                 }
    I'm pretty sure your 'c' values are going negative. Check it out with some print statements.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I count 10 obvious bugs at first glance. Any advances on that?

    The value of c will go rapidly negative and write crap all over the stack, probably crashing your program.

    If you're finding that people are unable to help you it is not that we can't see the bugs, it's that we can't see where there aren't bugs! The code has so many errors it is extremely difficult to determine exactly what it should do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM