Skip to content

Conversation

pniedzielski
Copy link
Collaborator

Previous PRs have added casts to silence warnings, but these warnings were in many cases real bugs. This PR will remove several casts that have the potential to introduce incorrect and exploitable behavior. Analyses of each are given in the commit messages in this PR.

This patch removes a narrowing static cast from `bsl::string::size_type`
to `int` which could cause an invalid configuration of sufficient length
to print an arbitrarily long error message.

When an invalid configuration is detected in the current behavior, we
statically cast its length from
`bsl::string::size_type` (a.k.a. `bsl::size_t`) to `int`.  If the JSON
configuration is too large, the results of this are implementation
defined; on many common implementations, though, this may result in a
negative `int`, depending on the exact length of the configuration.  We
then use `bsl::min` to limit it to a maximum of 1024, with the type of
the integral literal `1024` deduced to `int`.  If the casted `int` is
negative, it is propagated through.  Finally, we pass the
resulting `int` to a function that expects a `bsl::string::size_type`
again, causing another implementation-defined conversion.  Here again,
if the `int` is negative, we may get a very large value.  The
combination of a narrowing cast and the conversions between `unsigned`
and `signed` integers cause this pernitious behavior.

Instead of a cast and extra type conversions, this patch makes the
integral literal have the type `bsl::string::size_type`.  The value 1024
is representable with this type, so this is safe.  As a result, there
are no casts or other conversions, and the entire operation is done in
the input and result type `bsl::string::size_type`, avoiding this bug.

Signed-off-by: Patrick M. Niedzielski <[email protected]>
Copy link

@bmq-oss-ci bmq-oss-ci bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build 2612 of commit 5f9e4c1 has completed with FAILURE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant