Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

src: audit and fix shift-into-sign-bit bugs #34827

Copy link
Copy link
@bnoordhuis

Description

@bnoordhuis
Issue body actions
$ git grep -n  '<< 24' src/
src/base64.h:88:        unbase64(src[i + 0]) << 24 |
src/cares_wrap.cc:83:  return static_cast<uint32_t>(p[0] << 24U) |
src/node_sockaddr.cc:305:  uint32_t check = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
src/util-inl.h:51:  (((x) & 0xFF) << 24) |                                                      \
src/util-inl.h:61:  (((x) & 0x0000000000FF0000ull) << 24) |

The LHS here is usually of type unsigned char but gets promoted to int (because 24 is an int) and is then left-shifted.

If the LHS >= 128, that ends up shifting into the sign bit and that's implementation-defined behavior (i.e., bad - although it probably works okay with most compilers.)

Replace 24 with 24u and all is good. But! It's probably best to abstract it away into a helper function.

cares_wrap.cc already has one - cares_get_32bit() - that handles it correctly.

jasnell, juanarbol and ExE-Boss

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++Issues and PRs that require attention from people who are familiar with C++.Issues and PRs that require attention from people who are familiar with C++.good first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.