Skip to content

Commit

Permalink
Simplify dodgy pointer subtraction that caused occasional crashes now
Browse files Browse the repository at this point in the history
The first calculation was correct, although more complicated than
necessary. The second calculation was incorrect - it calculated a *byte*
count instead of a character count. This was previously not noticeable,
because std::wstring::assign (at least in the specific form that was
called here) respected string zero terminators. ST::string doesn't
though, causing buffer overruns and crashes sometimes.
  • Loading branch information
dgelessus committed Feb 18, 2023
1 parent 4d866db commit 42d8d15
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions Sources/Plasma/FeatureLib/pfJournalBook/pfJournalBook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1727,8 +1727,7 @@ bool pfJournalBook::ICompileSource(const ST::string& source, const plLocation
delete lastParChunk;
lastParChunk = nullptr;
} else if (lastParChunk) {
size_t count = ((uintptr_t)c - (uintptr_t)start) / sizeof(wchar_t); // wchar_t is 2 bytes
lastParChunk->fText = ST::string(start, count);
lastParChunk->fText = ST::string(start, c - start);
fHTMLSource.emplace_back(lastParChunk);
}

Expand Down Expand Up @@ -2093,8 +2092,7 @@ bool pfJournalBook::ICompileSource(const ST::string& source, const plLocation
delete lastParChunk;
lastParChunk = nullptr;
} else if (lastParChunk) {
size_t count = (uintptr_t)c - (uintptr_t)start;
lastParChunk->fText = ST::string(start, count);
lastParChunk->fText = ST::string(start, c - start);

fHTMLSource.emplace_back(lastParChunk);
}
Expand Down

0 comments on commit 42d8d15

Please sign in to comment.