1
我在做一個密碼驗證程序C++
,它基本上把一個字符串作爲輸入,然後再驗證一個字符串來確認它是正確的,並最終檢查輸入的字符串格式是否正確。假設字符串的長度大於6且小於20個字符,則至少包含1個大寫字母和1個小寫字母的字符串被稱爲「格式正確」。我終於寫出了這段代碼:*** ./pw中的錯誤:free():無效指針:0x0000000000602200 ***
#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";
}
我用終端中的普通g ++ pwcheck.cpp-o pw命令進行編譯! 我運行程序進行測試,輸入ILoveStackoverflow
作爲輸入,然後輸入相同的字符串來驗證2個匹配。 輸出是有些出人意料如下:
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)
我不知道發生了什麼確切發生的事情!任何幫助將大大appreaciated ..