FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add some comments explaining the switch between strdup and malloc/mem… · nodegit/nodegit@c866391 · GitHub

Repository navigation

Commit c866391

Browse files
committed
Add some comments explaining the switch between strdup and malloc/memcpy/memset.
1 parent 74e85a5 commit c866391

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

‎generate/templates/partials/convert_from_v8.cc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414
{%if cppClassName == 'String'%}
1515

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

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

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

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL