Skip to content

Commit bcef014

Browse files
authored
Merge pull request #37 from finger563/fix/string-deserialization-with-default-value
fix(string): fix deserialization of string with default value
2 parents 6bed0a0 + e424f7c commit bcef014

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

include/alpaca/detail/types/string.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ typename std::enable_if<!std::is_same_v<Container, std::ifstream>, bool>::type
3838
from_bytes(std::basic_string<CharType> &value, Container &bytes,
3939
std::size_t &current_index, std::size_t &end_index,
4040
std::error_code &error_code) {
41-
41+
// clear out the value - this ensures that value will be only what is read
42+
// from the stream, and not any previous data that may have been set during
43+
// the construction of the containing object T().
44+
value.clear();
4245
if (current_index >= end_index) {
4346
// end of input
4447
// return true for forward compatibility
@@ -75,7 +78,10 @@ typename std::enable_if<std::is_same_v<Container, std::ifstream>, bool>::type
7578
from_bytes(std::basic_string<CharType> &value, Container &bytes,
7679
std::size_t &current_index, std::size_t &end_index,
7780
std::error_code &error_code) {
78-
81+
// clear out the value - this ensures that value will be only what is read
82+
// from the stream, and not any previous data that may have been set during
83+
// the construction of the containing object T().
84+
value.clear();
7985
if (current_index >= end_index) {
8086
// end of input
8187
// return true for forward compatibility
@@ -109,4 +115,4 @@ from_bytes(std::basic_string<CharType> &value, Container &bytes,
109115
} // namespace detail
110116

111117
} // namespace alpaca
112-
#endif
118+
#endif

0 commit comments

Comments
 (0)