8000 Switch from strdup to memcpy to faithfully copy all bytes and prevent a crash in some cases. by tylerchurch · Pull Request #966 · nodegit/nodegit · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions generate/templates/partials/convert_from_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
{%if cppClassName == 'String'%}

String::Utf8Value {{ name }}(info[{{ jsArg }}]->ToString());
from_{{ name }} = ({{ cType }}) strdup(*{{ name }});
// malloc with one extra byte so we can add the terminating null character C-strings expect:
from_{{ name }} = ({{ cType }}) malloc({{ name }}.length() + 1);
// copy the characters from the nodejs string into our C-string (used instead of strdup or strcpy because nulls in
// the middle of strings are valid coming from nodejs):
memcpy((void *)from_{{ name }}, *{{ name }}, {{ name }}.length());
// ensure the final byte of our new string is null, extra casts added to ensure compatibility with various C types
// used in the nodejs binding generation:
memset((void *)(((char *)from_{{ name }}) + {{ name }}.length()), 0, 1);
{%elsif cppClassName == 'GitStrarray' %}

from_{{ name }} = StrArrayConverter::Convert(info[{{ jsArg }}]);
Expand All @@ -24,7 +31,14 @@
{%elsif cppClassName == 'Wrapper'%}

String::Utf8Value {{ name }}(info[{{ jsArg }}]->ToString());
from_{{ name }} = ({{ cType }}) strdup(*{{ name }});
// malloc with one extra byte so we can add the terminating null character C-strings expect:
from_{{ name }} = ({{ cType }}) malloc({{ name }}.length() + 1);
// copy the characters from the nodejs string into our C-string (used instead of strdup or strcpy because nulls in
// the middle of strings are valid coming from nodejs):
memcpy((void *)from_{{ name }}, *{{ name }}, {{ name }}.length());
// ensure the final byte of our new string is null, extra casts added to ensure compatibility with various C types
// used in the nodejs binding generation:
memset((void *)(((char *)from_{{ name }}) + {{ name }}.length()), 0, 1);
{%elsif cppClassName == 'Array'%}

Array *tmp_{{ name }} = Array::Cast(*info[{{ jsArg }}]);
Expand Down
0