Skip to content

Commit

Permalink
vst3: fix compiler warning in Vst3Plugin::Processor (fixes #6)
Browse files Browse the repository at this point in the history
If the `read_stream()` method ever would have been called with a
non-positive `max_size` value, it could have done unsafe things. But
this theoretical warning suggests reconsidering a more fundamental
problem: actually, there is no point in this method being a
general-purpose stream reader in the first place, receiving the buffer
size as a parameter, since it is only ever called with a constant value
(`Serializer::MAX_SIZE`). Premature abstraction is the other root of all
evil, so let's just rename it to `read_serialized_patch()`, and get rid
of the general-purpose `max_size` parameter.
  • Loading branch information
attilammagyar committed May 9, 2023
1 parent 52829ee commit 0730f01
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
16 changes: 7 additions & 9 deletions src/plugin/vst3/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ tresult PLUGIN_API Vst3Plugin::Processor::setState(IBStream* state)
return kResultFalse;
}

std::string const serialized = read_stream(state, Serializer::MAX_SIZE);
std::string const serialized = read_serialized_patch(state);

synth.process_messages();
Serializer::import(&synth, serialized);
Expand All @@ -491,10 +491,8 @@ tresult PLUGIN_API Vst3Plugin::Processor::setState(IBStream* state)
}


std::string Vst3Plugin::Processor::read_stream(
IBStream* stream,
Integer const max_size
) const {
std::string Vst3Plugin::Processor::read_serialized_patch(IBStream* stream) const
{
/*
Not using FStreamer::readString8(), because we need the entire string here,
and that method stops at line breaks, handles Unix-style line breaks
Expand All @@ -504,12 +502,12 @@ std::string Vst3Plugin::Processor::read_stream(
https://github.com/steinbergmedia/vst3_base/pull/5
*/

char* buffer = new char[max_size];
char* buffer = new char[Serializer::MAX_SIZE];
Integer i;
int32 numBytesRead;
char8 c;

for (i = 0; i != max_size; ++i) {
for (i = 0; i != Serializer::MAX_SIZE; ++i) {
stream->read(&c, sizeof(char8), &numBytesRead);

if (numBytesRead != sizeof(char8) || c == '\x00') {
Expand All @@ -519,8 +517,8 @@ std::string Vst3Plugin::Processor::read_stream(
buffer[i] = c;
}

if (i >= max_size) {
i = max_size - 1;
if (i >= Serializer::MAX_SIZE) {
i = Serializer::MAX_SIZE - 1;
}

buffer[i] = '\x00';
Expand Down
5 changes: 1 addition & 4 deletions src/plugin/vst3/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ class Vst3Plugin

Sample const* const* render_next_round(Integer const sample_count) noexcept;

std::string read_stream(
IBStream* stream,
Integer const max_size
) const;
std::string read_serialized_patch(IBStream* stream) const;

Synth synth;
Integer round;
Expand Down

0 comments on commit 0730f01

Please sign in to comment.