Skip to content

Commit

Permalink
Add and use a function to get string_view lengths as an int
Browse files Browse the repository at this point in the history
The .*s formatter needs the length of the subsequent string as a (signed) int,
but a string_view uses size_t for the length, which is an unsigned integer and
(potentially) larger. So the length has to be clamped to not overflow the int
and wrap to negative when cast.
  • Loading branch information
kcat committed Jan 28, 2024
1 parent d7cafac commit 700b4dc
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 97 deletions.
6 changes: 3 additions & 3 deletions al/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "alc/context.h"
#include "alc/inprogext.h"
#include "alspan.h"
#include "alstring.h"
#include "auxeffectslot.h"
#include "buffer.h"
#include "core/logging.h"
Expand Down Expand Up @@ -185,7 +186,7 @@ void ALCcontext::sendDebugMessage(std::unique_lock<std::mutex> &debuglock, Debug
if(message.length() >= MaxDebugMessageLength) UNLIKELY
{
ERR("Debug message too long (%zu >= %d):\n-> %.*s\n", message.length(),
MaxDebugMessageLength, static_cast<int>(message.length()), message.data());
MaxDebugMessageLength, al::sizei(message), message.data());
return;
}

Expand Down Expand Up @@ -226,8 +227,7 @@ void ALCcontext::sendDebugMessage(std::unique_lock<std::mutex> &debuglock, Debug
" Severity: %s\n"
" Message: \"%.*s\"\n",
GetDebugSourceName(source), GetDebugTypeName(type), id,
GetDebugSeverityName(severity), static_cast<int>(message.length()),
message.data());
GetDebugSeverityName(severity), al::sizei(message), message.data());
}
}

Expand Down
6 changes: 3 additions & 3 deletions al/eax/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cassert>
#include <exception>

#include "alstring.h"
#include "core/logging.h"


Expand All @@ -17,10 +18,9 @@ void eax_log_exception(std::string_view message) noexcept
std::rethrow_exception(exception_ptr);
}
catch(const std::exception& ex) {
const auto ex_message = ex.what();
ERR("%.*s %s\n", static_cast<int>(message.length()), message.data(), ex_message);
ERR("%.*s %s\n", al::sizei(message), message.data(), ex.what());
}
catch(...) {
ERR("%.*s %s\n", static_cast<int>(message.length()), message.data(), "Generic exception.");
ERR("%.*s %s\n", al::sizei(message), message.data(), "Generic exception.");
}
}
2 changes: 1 addition & 1 deletion al/effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ void LoadReverbPreset(const std::string_view name, ALeffect *effect)
return;
}

WARN("Reverb preset '%.*s' not found\n", std::abs(static_cast<int>(name.size())), name.data());
WARN("Reverb preset '%.*s' not found\n", al::sizei(name), name.data());
}

bool IsValidEffectType(ALenum type) noexcept
Expand Down
36 changes: 3 additions & 33 deletions alc/alc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,7 @@ void alc_initconfig()
else if(al::case_compare(entry, "neon"sv) == 0)
capfilter &= ~CPU_CAP_NEON;
else
WARN("Invalid CPU extension \"%.*s\"\n", std::abs(static_cast<int>(entry.size())),
entry.data());

WARN("Invalid CPU extension \"%.*s\"\n", al::sizei(entry), entry.data());
}
}
if(auto cpuopt = GetCPUInfo())
Expand Down Expand Up @@ -2855,29 +2853,10 @@ ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName) noexcep
return nullptr;
}

/* We need to ensure the device name isn't too long. The string_view is
* printed using the "%.*s" formatter, which uses an int for the precision/
* length. It wouldn't be a significant problem if larger values simply
* printed fewer characters due to truncation, but negative values are
* ignored, treating it like a normal null-terminated string, and
* string_views don't need to be null-terminated.
*
* Other than the annoyance of checking, this shouldn't be a problem. Two
* billion bytes is enough for a device name.
*/
std::string_view devname{deviceName ? deviceName : ""};
if(!devname.empty())
{
if(devname.length() >= std::numeric_limits<int>::max())
{
ERR("Device name too long (%zu >= %d)\n", devname.length(),
std::numeric_limits<int>::max());
alcSetError(nullptr, ALC_INVALID_VALUE);
return nullptr;
}

TRACE("Opening playback device \"%.*s\"\n", static_cast<int>(devname.size()),
devname.data());
TRACE("Opening playback device \"%.*s\"\n", al::sizei(devname), devname.data());
if(al::case_compare(devname, GetDefaultName()) == 0
#ifdef _WIN32
/* Some old Windows apps hardcode these expecting OpenAL to use a
Expand Down Expand Up @@ -3023,16 +3002,7 @@ ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName,
std::string_view devname{deviceName ? deviceName : ""};
if(!devname.empty())
{
if(devname.length() >= std::numeric_limits<int>::max())
{
ERR("Device name too long (%zu >= %d)\n", devname.length(),
std::numeric_limits<int>::max());
alcSetError(nullptr, ALC_INVALID_VALUE);
return nullptr;
}

TRACE("Opening capture device \"%.*s\"\n", static_cast<int>(devname.size()),
devname.data());
TRACE("Opening capture device \"%.*s\"\n", al::sizei(devname), devname.data());
if(al::case_compare(devname, GetDefaultName()) == 0
|| al::case_compare(devname, "openal-soft"sv) == 0)
devname = {};
Expand Down
3 changes: 1 addition & 2 deletions alc/alconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ void LoadConfigFromFile(std::istream &f)
}
}

TRACE(" setting '%s' = '%.*s'\n", fullKey.c_str(), static_cast<int>(valpart.size()),
valpart.data());
TRACE(" setting '%s' = '%.*s'\n", fullKey.c_str(), al::sizei(valpart), valpart.data());

/* Check if we already have this option set */
auto find_key = [&fullKey](const ConfigEntry &entry) -> bool
Expand Down
5 changes: 3 additions & 2 deletions alc/backends/alsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "alc/alconfig.h"
#include "almalloc.h"
#include "alnumeric.h"
#include "alstring.h"
#include "althrd_setname.h"
#include "core/device.h"
#include "core/helpers.h"
Expand Down Expand Up @@ -673,7 +674,7 @@ void AlsaPlayback::open(std::string_view name)
[name](const DevMap &entry) -> bool { return entry.name == name; });
if(iter == PlaybackDevices.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()), name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};
driver = iter->device_name;
}
else
Expand Down Expand Up @@ -942,7 +943,7 @@ void AlsaCapture::open(std::string_view name)
[name](const DevMap &entry) -> bool { return entry.name == name; });
if(iter == CaptureDevices.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()), name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};
driver = iter->device_name;
}
else
Expand Down
9 changes: 5 additions & 4 deletions alc/backends/coreaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <optional>

#include "alnumeric.h"
#include "alstring.h"
#include "core/converter.h"
#include "core/device.h"
#include "core/logging.h"
Expand Down Expand Up @@ -376,7 +377,7 @@ void CoreAudioPlayback::open(std::string_view name)
auto devmatch = std::find_if(PlaybackList.cbegin(), PlaybackList.cend(), find_name);
if(devmatch == PlaybackList.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()), name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};

audioDevice = devmatch->mId;
}
Expand All @@ -385,7 +386,7 @@ void CoreAudioPlayback::open(std::string_view name)
name = ca_device;
else if(name != ca_device)
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
static_cast<int>(name.length()), name.data()};
al::sizei(name), name.data()};
#endif

/* open the default output unit */
Expand Down Expand Up @@ -675,7 +676,7 @@ void CoreAudioCapture::open(std::string_view name)
auto devmatch = std::find_if(CaptureList.cbegin(), CaptureList.cend(), find_name);
if(devmatch == CaptureList.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()), name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};

audioDevice = devmatch->mId;
}
Expand All @@ -684,7 +685,7 @@ void CoreAudioCapture::open(std::string_view name)
name = ca_device;
else if(name != ca_device)
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
static_cast<int>(name.length()), name.data()};
al::sizei(name), name.data()};
#endif

AudioComponentDescription desc{};
Expand Down
7 changes: 3 additions & 4 deletions alc/backends/dsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "albit.h"
#include "alnumeric.h"
#include "alspan.h"
#include "alstring.h"
#include "althrd_setname.h"
#include "comptr.h"
#include "core/device.h"
Expand Down Expand Up @@ -330,8 +331,7 @@ void DSoundPlayback::open(std::string_view name)
[&id](const DevMap &entry) -> bool { return entry.guid == id; });
if(iter == PlaybackDevices.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()),
name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};
}
guid = &iter->guid;
}
Expand Down Expand Up @@ -602,8 +602,7 @@ void DSoundCapture::open(std::string_view name)
[&id](const DevMap &entry) -> bool { return entry.guid == id; });
if(iter == CaptureDevices.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()),
name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};
}
guid = &iter->guid;
}
Expand Down
3 changes: 2 additions & 1 deletion alc/backends/jack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "alc/alconfig.h"
#include "alnumeric.h"
#include "alsem.h"
#include "alstring.h"
#include "althrd_setname.h"
#include "core/device.h"
#include "core/helpers.h"
Expand Down Expand Up @@ -484,7 +485,7 @@ void JackPlayback::open(std::string_view name)
auto iter = std::find_if(PlaybackList.cbegin(), PlaybackList.cend(), check_name);
if(iter == PlaybackList.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()), name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};
mPortPattern = iter->mPattern;
}

Expand Down
5 changes: 3 additions & 2 deletions alc/backends/null.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
#include <functional>
#include <thread>

#include "althrd_setname.h"
#include "almalloc.h"
#include "alstring.h"
#include "althrd_setname.h"
#include "core/device.h"
#include "core/helpers.h"

Expand Down Expand Up @@ -110,7 +111,7 @@ void NullBackend::open(std::string_view name)
name = GetDeviceName();
else if(name != GetDeviceName())
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
static_cast<int>(name.length()), name.data()};
al::sizei(name), name.data()};

mDevice->DeviceName = name;
}
Expand Down
5 changes: 3 additions & 2 deletions alc/backends/oboe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstring>

#include "alnumeric.h"
#include "alstring.h"
#include "core/device.h"
#include "core/logging.h"
#include "ringbuffer.h"
Expand Down Expand Up @@ -63,7 +64,7 @@ void OboePlayback::open(std::string_view name)
name = GetDeviceName();
else if(name != GetDeviceName())
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
static_cast<int>(name.length()), name.data()};
al::sizei(name), name.data()};

/* Open a basic output stream, just to ensure it can work. */
oboe::ManagedStream stream;
Expand Down Expand Up @@ -243,7 +244,7 @@ void OboeCapture::open(std::string_view name)
name = GetDeviceName();
else if(name != GetDeviceName())
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
static_cast<int>(name.length()), name.data()};
al::sizei(name), name.data()};

oboe::AudioStreamBuilder builder;
builder.setDirection(oboe::Direction::Input)
Expand Down
5 changes: 3 additions & 2 deletions alc/backends/opensl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "albit.h"
#include "alnumeric.h"
#include "alsem.h"
#include "alstring.h"
#include "althrd_setname.h"
#include "core/device.h"
#include "core/helpers.h"
Expand Down Expand Up @@ -318,7 +319,7 @@ void OpenSLPlayback::open(std::string_view name)
name = GetDeviceName();
else if(name != GetDeviceName())
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
static_cast<int>(name.length()), name.data()};
al::sizei(name), name.data()};

/* There's only one device, so if it's already open, there's nothing to do. */
if(mEngineObj) return;
Expand Down Expand Up @@ -622,7 +623,7 @@ void OpenSLCapture::open(std::string_view name)
name = GetDeviceName();
else if(name != GetDeviceName())
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
static_cast<int>(name.length()), name.data()};
al::sizei(name), name.data()};

SLresult result{slCreateEngine(&mEngineObj, 0, nullptr, 0, nullptr, nullptr)};
PrintErr(result, "slCreateEngine");
Expand Down
5 changes: 3 additions & 2 deletions alc/backends/oss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "alc/alconfig.h"
#include "almalloc.h"
#include "alnumeric.h"
#include "alstring.h"
#include "althrd_setname.h"
#include "core/device.h"
#include "core/helpers.h"
Expand Down Expand Up @@ -353,7 +354,7 @@ void OSSPlayback::open(std::string_view name)
);
if(iter == PlaybackDevices.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()), name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};
devname = iter->device_name.c_str();
}

Expand Down Expand Up @@ -554,7 +555,7 @@ void OSScapture::open(std::string_view name)
);
if(iter == CaptureDevices.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()), name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};
devname = iter->device_name.c_str();
}

Expand Down
4 changes: 2 additions & 2 deletions alc/backends/pipewire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ void PipeWirePlayback::open(std::string_view name)
auto match = std::find_if(devlist.cbegin(), devlist.cend(), match_name);
if(match == devlist.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()), name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};

targetid = match->mSerial;
devname = match->mName;
Expand Down Expand Up @@ -2015,7 +2015,7 @@ void PipeWireCapture::open(std::string_view name)
}
if(match == devlist.cend())
throw al::backend_exception{al::backend_error::NoDevice,
"Device name \"%.*s\" not found", static_cast<int>(name.length()), name.data()};
"Device name \"%.*s\" not found", al::sizei(name), name.data()};

targetid = match->mSerial;
devname = name;
Expand Down
5 changes: 3 additions & 2 deletions alc/backends/portaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "albit.h"
#include "alc/alconfig.h"
#include "alnumeric.h"
#include "alstring.h"
#include "core/device.h"
#include "core/logging.h"
#include "dynload.h"
Expand Down Expand Up @@ -115,7 +116,7 @@ void PortPlayback::open(std::string_view name)
name = GetDefaultName();
else if(name != GetDefaultName())
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
static_cast<int>(name.length()), name.data()};
al::sizei(name), name.data()};

PaStreamParameters params{};
auto devidopt = ConfigValueInt({}, "port", "device");
Expand Down Expand Up @@ -268,7 +269,7 @@ void PortCapture::open(std::string_view name)
name = GetDefaultName();
else if(name != GetDefaultName())
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
static_cast<int>(name.length()), name.data()};
al::sizei(name), name.data()};

uint samples{mDevice->BufferSize};
samples = maxu(samples, 100 * mDevice->Frequency / 1000);
Expand Down

0 comments on commit 700b4dc

Please sign in to comment.