Skip to content

Commit 2cc2280

Browse files
committed
Fix invalid write in String::asBase64(), add tests
Signed-off-by: falkTX <falktx@falktx.com>
1 parent d11070d commit 2cc2280

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

distrho/extra/String.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ class String
699699
char* strBuf = static_cast<char*>(std::malloc(strBufSize));
700700
DISTRHO_SAFE_ASSERT_RETURN(strBuf != nullptr, String());
701701

702-
strBuf[strBufSize] = '\0';
702+
strBuf[strBufSize - 1] = '\0';
703703
size_t strBufIndex = 0;
704704

705705
const uchar* bytesToEncode = static_cast<const uchar*>(data);

tests/Base64.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* DISTRHO Plugin Framework (DPF)
3+
* Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
4+
*
5+
* Permission to use, copy, modify, and/or distribute this software for any purpose with
6+
* or without fee is hereby granted, provided that the above copyright notice and this
7+
* permission notice appear in all copies.
8+
*
9+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10+
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11+
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12+
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13+
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14+
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
#define HEADLESS_TESTS
18+
#include "tests.hpp"
19+
#include "distrho/extra/Base64.hpp"
20+
#include "distrho/extra/String.hpp"
21+
22+
// --------------------------------------------------------------------------------------------------------------------
23+
24+
int main()
25+
{
26+
using namespace DISTRHO_NAMESPACE;
27+
28+
String test("This is a test");
29+
std::vector<uint8_t> testblob(test.buffer(), test.buffer() + (test.length() + 1));
30+
std::vector<uint8_t> blob = d_getChunkFromBase64String(String::asBase64(test.buffer(), test.length() + 1));
31+
DISTRHO_ASSERT_EQUAL(blob, testblob, "encode + decode must match original data");
32+
33+
return 0;
34+
}
35+
36+
// --------------------------------------------------------------------------------------------------------------------

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ endif
2424
# ---------------------------------------------------------------------------------------------------------------------
2525

2626
MANUAL_TESTS =
27-
UNIT_TESTS = Color Point
27+
UNIT_TESTS = Base64 Color Point
2828

2929
ifeq ($(HAVE_CAIRO),true)
3030
MANUAL_TESTS += Demo.cairo

tests/tests.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* DISTRHO Plugin Framework (DPF)
3-
* Copyright (C) 2012-2024 Filipe Coelho <falktx@falktx.com>
3+
* Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
44
*
55
* Permission to use, copy, modify, and/or distribute this software for any purpose with
66
* or without fee is hereby granted, provided that the above copyright notice and this
@@ -16,10 +16,6 @@
1616

1717
#pragma once
1818

19-
#include "dgl/Application.hpp"
20-
21-
#include "distrho/extra/Thread.hpp"
22-
2319
#define DISTRHO_ASSERT_EQUAL(v1, v2, msg) \
2420
if (v1 != v2) { d_stderr2("Test condition failed: %s; file:%s line:%i", msg, __FILE__, __LINE__); return 1; }
2521

@@ -29,6 +25,11 @@
2925
#define DISTRHO_ASSERT_SAFE_EQUAL(v1, v2, msg) \
3026
if (d_isNotEqual(v1, v2)) { d_stderr2("Test condition failed: %s; file:%s line:%i", msg, __FILE__, __LINE__); return 1; }
3127

28+
#ifndef HEADLESS_TESTS
29+
30+
#include "dgl/Application.hpp"
31+
#include "distrho/extra/Thread.hpp"
32+
3233
START_NAMESPACE_DGL
3334

3435
// --------------------------------------------------------------------------------------------------------------------
@@ -59,3 +60,5 @@ class ApplicationQuitter : public Thread
5960
// --------------------------------------------------------------------------------------------------------------------
6061

6162
END_NAMESPACE_DGL
63+
64+
#endif

0 commit comments

Comments
 (0)