2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-09 00:27:42 +00:00

CStringExtras: Prevent potential out of bounds reads with CompareCaseInsensitive

The strcasecmp and _stricmp functions expect the passed in strings to
be null-terminated, however we we're also exposing a std::string_view
overload for that function. std::string_view instances aren't required
to be null-terminated, so this makes the interface a little unsafe.

We can use std::lexicographical_compare() to provide the same behavior
and also properly handle the case of non-null-terminated strings.
This commit is contained in:
Lioncash
2019-09-14 11:14:51 -04:00
parent 002ca71104
commit 0d2b2f45a8
5 changed files with 30 additions and 18 deletions

View File

@@ -862,7 +862,7 @@ void CStreamAudioManager::Start(bool oneshot, std::string_view fileName, float v
SDSPPlayer& p = s_Players[oneshot];
SDSPPlayer& qp = s_QueuedPlayers[oneshot];
if (p.x10_playState != EPlayerState::Stopped && CStringExtras::CompareCaseInsensitive(fileName, p.x0_fileName)) {
if (p.x10_playState != EPlayerState::Stopped && !CStringExtras::CompareCaseInsensitive(fileName, p.x0_fileName)) {
/* Enque new stream */
qp = SDSPPlayer(EPlayerState::FadeIn, fileName, volume, fadeIn, fadeOut, -1, music);
Stop(oneshot, p.x0_fileName);
@@ -900,10 +900,10 @@ void CStreamAudioManager::Stop(bool oneshot, std::string_view fileName) {
SDSPPlayer& p = s_Players[oneshot];
SDSPPlayer& qp = s_QueuedPlayers[oneshot];
if (!CStringExtras::CompareCaseInsensitive(fileName, qp.x0_fileName)) {
if (CStringExtras::CompareCaseInsensitive(fileName, qp.x0_fileName)) {
/* Cancel enqueued file */
qp = SDSPPlayer();
} else if (!CStringExtras::CompareCaseInsensitive(fileName, p.x0_fileName) && p.x20_internalHandle != -1 &&
} else if (CStringExtras::CompareCaseInsensitive(fileName, p.x0_fileName) && p.x20_internalHandle != -1 &&
p.x10_playState != EPlayerState::Stopped) {
/* Fade out or stop */
if (p.x1c_fadeOut <= FLT_EPSILON)