Skip to content

Commit

Permalink
Add some comments explaining the switch between strdup and malloc/mem…
Browse files Browse the repository at this point in the history
…cpy/memset.
  • Loading branch information
tylerc committed Mar 24, 2016
1 parent 74e85a5 commit c866391
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions generate/templates/partials/convert_from_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
{%if cppClassName == 'String'%}

String::Utf8Value {{ name }}(info[{{ jsArg }}]->ToString());
// 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' %}

Expand All @@ -26,8 +31,13 @@
{%elsif cppClassName == 'Wrapper'%}

String::Utf8Value {{ name }}(info[{{ jsArg }}]->ToString());
// 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'%}

Expand Down

0 comments on commit c866391

Please sign in to comment.