Reputation: 79
I was trying to solve leetcode problem "929. Unique Email Addresses", the code works fine at my computer on Visual Studio Code but when I pasted it on the leetcode, I got address sanitizer heap-buffer-overflow error. The code is shown below:
class Solution {
public:
int numUniqueEmails(vector<string>& emails) {
string::iterator it;
for (int i = 0; i < emails.size(); i++) {
for (it = emails[i].begin(); *it != '@'; it++) {
if (*it == '.') {
emails[i].erase(it);
it--;
}
if (*it == '+') {
while (*it != '@') {
emails[i].erase(it);
}
break;
}
}
sort(emails.begin(), emails.end());
for (int j = 0; j < emails.size(); j++) {
if (emails[j] == emails[j + 1]) {
emails.erase(emails.begin() + j);
j--;
}
}
}
return emails.size();
}
};
Can someone let me know why the error occurs?
Upvotes: 6
Views: 4985
Reputation: 73587
Because the following loops:
for (it = emails[i].begin(); *it != '@'; it++)
...
and
while (*it != '@')
...
iterate with it
over the string without never ever verifying if the end of the string was reached. If you have a single ill-formatted email address in the input data, you therefore go out of bounds. So UB.
In addition, you also go out of bounds here:
for (int j = 0; j < emails.size(); j++) {
if (emails[j] == emails[j + 1]) { // what when j == size-1 ????
Finally a potentially nasty issue as well (but could affect the result): your first for
loop should end before the sort()
. Why? because with some bad luck, the sorting will change the order of the elements, causing an unprocessed address to move before the current i index and thus remain unprocessed.
Demo with some additional display of intermediary results and some extreme cases.
Upvotes: 1