user5864689
user5864689

Reputation: 39

*** Error in `./pw': free(): invalid pointer: 0x0000000000602200 ***

I was doing a password Validator program in C++ which basically takes a string pass as input and then another string verify to confirm that it got it right and finally checks that if the string entered is in correct format or not. A string with atleast 1 uppercase and 1 lowercase letter is called to be in "correct format" given that the length of the string is greater than 6 and smaller than 20 charecters. I finally came up writing this snippet of code :

#include <iostream>
using namespace std;
string pass, verify;
int cntUpper = 0, cntLower = 0;
int cntTry = 3;
int notValid();
int input();
string verify_format(string ab, string bc);


int main()
{
    for (int i = 0; i <= 50; i++)
    {
        cout << "*";
    }
    cout << endl;
    cout << "Welcome to password Validator\n";  
    for (int g = 0; g <= 50; g++)
    {
        cout << "*";
    }
    cout << endl;
    input();
    cin.ignore();
    return 0;
}   

int notValid(int a, int b) 
{   cout << "\nYour password is not valid\n";
    if (cntTry > 0)
    {
        cout << "Please try again. You have " << cntTry << " chances left\n";
        cntTry--;
        input();
    }
    else 
    {
        cout << "You reached your maximum limits. Aborting...\n";
        return 0;
    }

}

int input() {
cout << "Enter your password : ";
    getline(cin, pass);
    cout << "Verify : ";
    getline(cin, verify);
    verify_format(pass, verify);

}


string verify_format(string ab, string bc){
if (ab != bc)
    {
        cout << "Password do not matches. Try again!\n";
        input();
    }
else cout <<"\nPass Matched\n";
    for (int i = 0; i < pass.size(); i++)
    {
        if (isupper(pass.at(i)))
            cntUpper++;     
    }
    for (int v = 0; v < pass.size(); v++)
    {
        if (islower(pass.at(v)))
            cntLower++;
    }
    if (pass.size() > 20 || pass.size() < 6 || cntUpper == 0 || cntLower == 0)
        notValid(cntLower, cntUpper);

    else cout << "Password Accepted!\n";

}

I compiled in with usual g++ pwcheck.cpp-o pw command in the terminal! I ran the program for testing and entered ILoveStackoverflow as the input and then entered the same string to verify that the 2 stings matches. The output was somewhat unexpected as follow :

Pass Matched
Password Accepted!
*** Error in `./pw': free(): invalid pointer: 0x0000000000602200 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fba2b7627e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7fba2b76b37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fba2b76f53c]
./pw[0x401149]
./pw[0x401053]
./pw[0x40130f]
./pw[0x40113d]
./pw[0x400fd4]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fba2b70b830]
./pw[0x400e89]
======= Memory map: ========
00400000-00402000 r-xp 00000000 00:16 21169                              /home/lubuntu/pw
00601000-00602000 r--p 00001000 00:16 21169                              /home/lubuntu/pw
00602000-00603000 rw-p 00002000 00:16 21169                              /home/lubuntu/pw
022c4000-022f6000 rw-p 00000000 00:00 0                                  [heap]
7fba24000000-7fba24021000 rw-p 00000000 00:00 0 
7fba24021000-7fba28000000 ---p 00000000 00:00 0 
7fba2b3e2000-7fba2b4ea000 r-xp 00000000 00:16 9384                       /lib/x86_64-linux-gnu/libm-2.23.so
7fba2b4ea000-7fba2b6e9000 ---p 00108000 00:16 9384                       /lib/x86_64-linux-gnu/libm-2.23.so
7fba2b6e9000-7fba2b6ea000 r--p 00107000 00:16 9384                       /lib/x86_64-linux-gnu/libm-2.23.so
7fba2b6ea000-7fba2b6eb000 rw-p 00108000 00:16 9384                       /lib/x86_64-linux-gnu/libm-2.23.so
7fba2b6eb000-7fba2b8ab000 r-xp 00000000 00:16 9390                       /lib/x86_64-linux-gnu/libc-2.23.so
7fba2b8ab000-7fba2baab000 ---p 001c0000 00:16 9390                       /lib/x86_64-linux-gnu/libc-2.23.so
7fba2baab000-7fba2baaf000 r--p 001c0000 00:16 9390                       /lib/x86_64-linux-gnu/libc-2.23.so
7fba2baaf000-7fba2bab1000 rw-p 001c4000 00:16 9390                       /lib/x86_64-linux-gnu/libc-2.23.so
7fba2bab1000-7fba2bab5000 rw-p 00000000 00:00 0 
7fba2bab5000-7fba2bacb000 r-xp 00000000 00:16 2192                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fba2bacb000-7fba2bcca000 ---p 00016000 00:16 2192                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fba2bcca000-7fba2bccb000 rw-p 00015000 00:16 2192                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fba2bccb000-7fba2be3d000 r-xp 00000000 00:16 2191                       /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fba2be3d000-7fba2c03d000 ---p 00172000 00:16 2191                       /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fba2c03d000-7fba2c047000 r--p 00172000 00:16 2191                       /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fba2c047000-7fba2c049000 rw-p 0017c000 00:16 2191                       /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fba2c049000-7fba2c04d000 rw-p 00000000 00:00 0 
7fba2c04d000-7fba2c073000 r-xp 00000000 00:16 9367                       /lib/x86_64-linux-gnu/ld-2.23.so
7fba2c259000-7fba2c25e000 rw-p 00000000 00:00 0 
7fba2c26f000-7fba2c272000 rw-p 00000000 00:00 0 
7fba2c272000-7fba2c273000 r--p 00025000 00:16 9367                       /lib/x86_64-linux-gnu/ld-2.23.so
7fba2c273000-7fba2c274000 rw-p 00026000 00:16 9367                       /lib/x86_64-linux-gnu/ld-2.23.so
7fba2c274000-7fba2c275000 rw-p 00000000 00:00 0 
7fffde86a000-7fffde88b000 rw-p 00000000 00:00 0                          [stack]
7fffde939000-7fffde93b000 r--p 00000000 00:00 0                          [vvar]
7fffde93b000-7fffde93d000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

I am not sure what's exactly happening there! Any help would be greatly appreaciated..

Upvotes: 1

Views: 119

Answers (1)

John Kugelman
John Kugelman

Reputation: 361605

int notValid(int a, int b);
int input();
string verify_format(string ab, string bc);

These functions claim to return ints and strings, but their implementations are notably lacking in return statements.

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function. (link)

Change them to void.

void notValid(int a, int b);
void input();
void verify_format(string ab, string bc);

Upvotes: 1

Related Questions