Skip to content

Invalid read in node::base64_decode<char>(char*, unsigned long, char const*, unsigned long)#2166

@kzc

Description

@kzc

Found this while valgrinding the worker threads implementation:

==32704== Invalid read of size 1 ==32704== at 0xD1E319: unsigned long node::base64_decode<char>(char*, unsigned long, char const*, unsigned long) ==32704== by 0xD1E8DD: node::StringBytes::Write(v8::Isolate*, char*, unsigned long, v8::Handle<v8::Value>, node::encoding, int*) ==32704== by 0xD3AF14: node::crypto::Hash::HashUpdate(v8::FunctionCallbackInfo<v8::Value> const&) ==32704== by 0x853B31: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) ==32704== by 0x87AD4A: v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) ==32704== Address 0x20e21598 is 0 bytes after a block of size 5,592,408 alloc'd ==32704== at 0x4C2B800: operator new[](unsigned long) ==32704== by 0xD1D7EA: node::StringBytes::Encode(v8::Isolate*, char const*, unsigned long, node::encoding) ==32704== by 0xCF6E06: node::Buffer::Base64Slice(v8::FunctionCallbackInfo<v8::Value> const&) ==32704== by 0x853B31: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) ==32704== by 0x87AD4A: v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) 

Likely fix:

--- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -167,5 +167,5 @@ size_t base64_decode(char* buf, int remaining = srcEnd - src; - while (unbase64(*src) < 0 && src < srcEnd) + while (src < srcEnd && unbase64(*src) < 0) src++, remaining--; if (remaining == 0 || *src == '=') @@ -173,5 +173,5 @@ size_t base64_decode(char* buf, a = unbase64(*src++); - while (unbase64(*src) < 0 && src < srcEnd) + while (src < srcEnd && unbase64(*src) < 0) src++, remaining--; if (remaining <= 1 || *src == '=') @@ -183,5 +183,5 @@ size_t base64_decode(char* buf, break; - while (unbase64(*src) < 0 && src < srcEnd) + while (src < srcEnd && unbase64(*src) < 0) src++, remaining--; if (remaining <= 2 || *src == '=') @@ -193,5 +193,5 @@ size_t base64_decode(char* buf, break; - while (unbase64(*src) < 0 && src < srcEnd) + while (src < srcEnd && unbase64(*src) < 0) src++, remaining--; if (remaining <= 3 || *src == '=') 

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++Issues and PRs that require attention from people who are familiar with C++.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions