Skip to content

Commit

Permalink
bdlsb_fixedmemoutput: fix ubsan problem (#4825)
Browse files Browse the repository at this point in the history
* bdlsb_fixedmemoutput: fix ubsan problem
  • Loading branch information
lalawawa authored and GitHub Enterprise committed Jul 16, 2024
1 parent c676486 commit f36c72c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
15 changes: 8 additions & 7 deletions groups/bdl/bdlsb/bdlsb_fixedmemoutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,16 @@ bsl::streamsize FixedMemOutput::sputn(const char *s,
bsl::streamsize length)
{
BSLS_ASSERT(s);
BSLS_ASSERT(0 <= length);

pos_type current = d_pos;
d_pos += length;
if (d_pos > d_capacity) {
d_pos = d_capacity;
length = static_cast<bsl::streamsize>(d_capacity - current);
length = bsl::min<bsl::streamsize>(length, d_capacity - d_pos);
if (0 < length) {
bsl::memcpy(d_buffer_p + static_cast<IntPtr>(d_pos), s, length);
d_pos += length;
}
else {
BSLS_ASSERT(0 == length);
}
bsl::memcpy(d_buffer_p + static_cast<IntPtr>(current), s, length);

return length;
}

Expand Down
18 changes: 13 additions & 5 deletions groups/bdl/bdlsb/bdlsb_fixedmemoutput.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,18 @@ int main(int argc, char **argv)
// This segment verifies correct behavior across different initial
// buffer states (buffer length x buffer contents.)
for(size_t i = 0; i < DATA_LEN; ++i ) {
const int LINE = DATA[i].d_line;
const TestData& data = DATA[i];
const int LINE = data.d_line;
const int CAP = data.d_strCap;
const int LEN = data.d_length;
const int RET = data.d_returnVal;

if (veryVerbose) {
P_(LINE); P_(CAP); P_(LEN); P(RET);
}

char *bytes = new char[DATA[i].d_strCap];
Obj mSB(bytes, DATA[i].d_strCap);
char *bytes = new char[CAP];
Obj mSB(bytes, CAP);
const Obj& SB = mSB;

for(unsigned j = 0; j < strlen(DATA[i].d_initCont); ++j ) {
Expand All @@ -739,8 +747,8 @@ int main(int argc, char **argv)
ASSERTV(LINE, 0 == strncmp(bytes,
DATA[i].d_result,
strlen(DATA[i].d_result )) );
ASSERTV(LINE, DATA[i].d_returnVal == retResult );
ASSERTV(LINE, DATA[i].d_length == SB.length());
ASSERTV(LINE, RET == retResult );
ASSERTV(LINE, LEN == SB.length());
delete [] bytes;
}
}
Expand Down

0 comments on commit f36c72c

Please sign in to comment.