Skip to content

Commit

Permalink
Fix uncaught exception caused by invalid UTF-8 data
Browse files Browse the repository at this point in the history
  • Loading branch information
windytan committed Jul 15, 2024
1 parent 3330480 commit 4147346
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: build

on:
push:
branches: [ master ]
branches: [ master, dev ]
tags: [ 'v*' ]
pull_request:
branches: [ master ]
Expand Down
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# redsea changelog

## 1.0.1 (2024-xx-xx)

* Fixes:
* Fix a crash (uncaught exception) when attempting to serialize a string that's
invalid UTF-8

## 1.0 (2024-06-27)

* New features:
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('redsea', 'cpp', default_options : ['warning_level=3', 'buildtype=release', 'optimization=3'],
version: '1.0')
version: '1.0.1')

# Store version number to be compiled in
conf = configuration_data()
Expand Down
13 changes: 12 additions & 1 deletion src/groups.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,18 @@ void Station::updateAndPrint(const Group& group, std::ostream& stream) {
}
}

stream << json_ << std::endl << std::flush;
try {
// nlohmann::operator<< throws if a string contains non-UTF8 data.
// It's better to throw while writing to a stringstream; otherwise
// incomplete JSON objects could get printed.
std::stringstream output_proxy_stream;
output_proxy_stream << json_ << std::endl << std::flush;
stream << output_proxy_stream.str();
} catch (const std::exception& e) {
nlohmann::ordered_json json_from_exception;
json_from_exception["debug"] = std::string(e.what());
stream << json_from_exception;
}
}

uint16_t Station::getPI() const {
Expand Down

0 comments on commit 4147346

Please sign in to comment.