Skip to content

Commit

Permalink
fix out-of-bounds read in genversion at a tagged commit
Browse files Browse the repository at this point in the history
+ merge the two git describe parsers
  • Loading branch information
cfillion committed Apr 17, 2024
1 parent d38c3d2 commit e53451b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
7 changes: 7 additions & 0 deletions src/vernum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ class VerNum {
return vernum;
}

constexpr ValT operator[](const unsigned char seg) const
{
if(seg >= MAX_SEGS)
throw reascript_error { "out of range segment" };
return (m_value >> ((MAX_SEGS - seg - 1) * SEG_BITS)) & SEG_MASK;
}

private:
ValT m_value;
};
Expand Down
15 changes: 15 additions & 0 deletions tests/vernum_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,18 @@ TEST(VerNumTest, FormatString) {
EXPECT_EQ(VerNum { "1.2.3.4" }.toString(), "1.2.3.4");
EXPECT_EQ(VerNum { "016.032.064.128" }.toString(), "16.32.64.128");
}

static void expectSegments(const VerNum &ver,
const std::array<decltype(ver[0]), 4> &segs)
{
for(size_t i {}; i < segs.size(); ++i)
EXPECT_EQ(ver[i], segs[i]);
}

TEST(VerNumTest, SegmentAccess) {
expectSegments("0", { 0, 0, 0, 0 });
expectSegments("1", { 1, 0, 0, 0 });
expectSegments("1.2", { 1, 2, 0, 0 });
expectSegments("1.2.3", { 1, 2, 3, 0 });
expectSegments("016.032.064.128", { 16, 32, 64, 128 });
}
55 changes: 14 additions & 41 deletions tools/genversion.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,24 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <algorithm>
#include <array>
#include <iostream>
#include <string_view>

using Segments = std::array<std::string_view, 4>;

static Segments explode(std::string_view version)
{
Segments segments;
for(std::string_view &segment : segments) {
const auto sep { std::find_if_not(version.begin(), version.end(),
[](char c) { return std::isdigit(c); }) };

// reached the end (version is empty) or segment is empty (consecutive dots)
if(sep == version.begin()) {
segment = "0";
continue;
}

// FIXME: C++20
// segment = { version.begin(), segEnd };
segment = { version.data(), static_cast<size_t>(sep - version.begin()) };

if(*sep == '.')
version.remove_prefix(segment.size() + 1);
else
version = {};
}

return segments;
}
#include "../api/compstr.hpp"
#include "../src/vernum.hpp"

int main()
{
// replaced by the output of `git describe`, eg. "v0.8.6.1-8-g19dcefc"
std::string_view version { "@VCS_TAG@" };

if(!version.empty() && version.front() == 'v')
version.remove_prefix(1);

const Segments segments { explode(version) };
std::cout << "#define REAIMGUI_VERSION \"" << version << "\"\n"
<< "#define REAIMGUI_VERSION_MAJOR " << segments[0] << '\n'
<< "#define REAIMGUI_VERSION_MINOR " << segments[1] << '\n'
<< "#define REAIMGUI_VERSION_PATCH " << segments[2] << '\n'
<< "#define REAIMGUI_VERSION_TWEAK " << segments[3] << '\n';
static constexpr const char gitDescribe[] { "@VCS_TAG@" };
constexpr VerNum version { CompStr::version<&gitDescribe> };

const char *tagVersion { gitDescribe };
if(*tagVersion == 'v')
++tagVersion;

std::cout << "#define REAIMGUI_VERSION \"" << tagVersion << "\"\n"
<< "#define REAIMGUI_VERSION_MAJOR " << version[0] << '\n'
<< "#define REAIMGUI_VERSION_MINOR " << version[1] << '\n'
<< "#define REAIMGUI_VERSION_PATCH " << version[2] << '\n'
<< "#define REAIMGUI_VERSION_TWEAK " << version[3] << '\n';
}
3 changes: 2 additions & 1 deletion tools/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ endif
genversion_src = vcs_tag(
command: [find_program('git'), 'describe', '--match=v*', '--dirty=+'],
input: 'genversion.cpp.in', output: 'genversion.cpp', fallback: '0.0-nogit')
genversion = executable('genversion', genversion_src, native: native)
genversion = executable('genversion', genversion_src, native: native,
dependencies: [compat_dep])
version = custom_target('version.hpp',
command: [genversion], capture: true, output: 'version.hpp')

Expand Down

0 comments on commit e53451b

Please sign in to comment.