Skip to content

Commit 95f8277

Browse files
committed
Use per-thread error storage (thanks to @chkothe)
1 parent 44d141d commit 95f8277

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/common.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#pragma comment(lib, "winmm.lib")
1313
#endif
1414

15-
char last_error[512];
15+
thread_local char last_error[512] = {0};
1616

1717
extern "C" {
1818

src/common.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern "C" {
3131
"Please do not compile this with a lslboost version older than 1.45 because the library would otherwise not be protocol-compatible with builds using other versions."
3232
#endif
3333

34-
extern char last_error[512];
34+
extern thread_local char last_error[512];
3535

3636
// the highest supported protocol version
3737
// * 100 is the original version, supported by library versions 1.00+

testing/test_ext_streaminfo.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
#include "catch.hpp"
22
#include <lsl_cpp.h>
3+
#include <thread>
34

45
namespace {
56

67
TEST_CASE("streaminfo", "[streaminfo][basic]") {
78
CHECK_THROWS(lsl::stream_info("", "emptyname"));
8-
CHECK(std::string("The name of a stream must be non-empty.") == lsl_last_error());
9+
REQUIRE(std::string("The name of a stream must be non-empty.") == lsl_last_error());
10+
}
11+
12+
TEST_CASE("multithreaded lsl_last_error", "[threading][basic]") {
13+
CHECK_THROWS(lsl::stream_info("", "emptyname"));
14+
std::thread([](){
15+
CHECK_THROWS(lsl::stream_info("hasname","type", -1));
16+
REQUIRE(std::string("The channel_count of a stream must be nonnegative.") == lsl_last_error());
17+
}).join();
18+
REQUIRE(std::string("The name of a stream must be non-empty.") == lsl_last_error());
19+
}
20+
21+
/// Ensure that an overly long error message won't overflow the buffer
22+
TEST_CASE("lsl_last_error size", "[basic]") {
23+
std::string invalidquery(512, '\'');
24+
CHECK_THROWS(lsl::resolve_stream(invalidquery, 1, 0.1));
25+
REQUIRE(lsl_last_error()[512] == 0);
926
}
1027
} // namespace

0 commit comments

Comments
 (0)