I was trying to solve a problem on Codeforces, I am trying to count the number of letters i have to remove so that no two neighbours have same alphabet.
#include<iostream>
#include<string>
#include<algorithm>
int main(){
int n,count=0,i=0,j=1;
std::cin>>n;
std::string s;
std::cin>>s;
if(n==1){
std::cout<<count;
}
else {
while(n--||s[j]){
if(s[i]==s[j]){
count++;
j++;
}
else {
i=j;
j=j+1;
}
}
std::cout<<count;
}
return 0;
}
I got runtime error. Diagnostics detected issues [cpp.clang++-diagnose]: =================================================================
==2064==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x1184f938 at pc 0x010a148f bp 0x1184f8f4 sp 0x1184f8f0 READ of size 1 at 0x1184f938 thread T0 ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_symbolizer_win.cc:64 "((dbghelp && "failed to load dbghelp.dll")) != (0)" (0x0, 0x0) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff) ==2064==AddressSanitizer CHECK failed: C:\src\llvm_package_600-final\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:795 "((owner_)) == ((LOCK_READY))" (0xe14, 0xffffffff)
The problem is with this line:
while(n--||s[j])
Now if n
is the length of the string, then s[j]
will result in an out-of-bounds memory access because j
is always incremented in the while
loop.
Take the simple case when the string is "me" and n
is 2
. s[2]
is not a legal access.
User contributions licensed under CC BY-SA 3.0