@@ -22,22 +22,18 @@ extern "C" {
2222
2323#include " php_v8js_macros.h"
2424
25- void v8js_commonjs_split_terms (char *identifier, std::vector<char *> &terms)
25+ static void v8js_commonjs_split_terms (const char *identifier, std::vector<char *> &terms)
2626{
27- char *term = (char *)malloc (PATH_MAX), *ptr = term;
28-
29- // Initialise the term string
30- *term = 0 ;
27+ char *term = (char *) emalloc (PATH_MAX), *ptr = term;
3128
3229 while (*identifier > 0 ) {
3330 if (*identifier == ' /' ) {
34- if (strlen (term) > 0 ) {
31+ if (ptr > term ) {
3532 // Terminate term string and add to terms vector
3633 *ptr++ = 0 ;
37- terms.push_back (strdup (term));
34+ terms.push_back (estrdup (term));
3835
3936 // Reset term string
40- memset (term, 0 , strlen (term));
4137 ptr = term;
4238 }
4339 } else {
@@ -47,18 +43,16 @@ void v8js_commonjs_split_terms(char *identifier, std::vector<char *> &terms)
4743 identifier++;
4844 }
4945
50- if (strlen (term) > 0 ) {
46+ if (ptr > term ) {
5147 // Terminate term string and add to terms vector
5248 *ptr++ = 0 ;
53- terms.push_back (strdup (term));
49+ terms.push_back (estrdup (term));
5450 }
5551
56- if (term > 0 ) {
57- free (term);
58- }
52+ efree (term);
5953}
6054
61- void v8js_commonjs_normalise_identifier (char *base, char *identifier, char *normalised_path, char *module_name)
55+ void v8js_commonjs_normalise_identifier (const char *base, const char *identifier, char *normalised_path, char *module_name)
6256{
6357 std::vector<char *> id_terms, terms;
6458 v8js_commonjs_split_terms (identifier, id_terms);
@@ -78,12 +72,19 @@ void v8js_commonjs_normalise_identifier(char *base, char *identifier, char *norm
7872 if (!strcmp (term, " .." )) {
7973 // Ignore parent term (..) if it's the first normalised term
8074 if (normalised_terms.size () > 0 ) {
81- // Remove the parent normalized term
75+ // Remove the parent normalized term (and free it)
76+ efree (normalised_terms.back ());
8277 normalised_terms.pop_back ();
8378 }
79+
80+ // free the ".." term
81+ efree (term);
8482 } else if (strcmp (term, " ." )) {
8583 // Add the term if it's not the current term (.)
8684 normalised_terms.push_back (term);
85+ } else {
86+ // Discard "." term
87+ efree (term);
8788 }
8889 }
8990
@@ -92,6 +93,8 @@ void v8js_commonjs_normalise_identifier(char *base, char *identifier, char *norm
9293 *module_name = 0 ;
9394
9495 strcat (module_name, normalised_terms.back ());
96+
97+ efree (normalised_terms.back ());
9598 normalised_terms.pop_back ();
9699
97100 for (std::vector<char *>::iterator it = normalised_terms.begin (); it != normalised_terms.end (); it++) {
@@ -102,5 +105,6 @@ void v8js_commonjs_normalise_identifier(char *base, char *identifier, char *norm
102105 }
103106
104107 strcat (normalised_path, term);
108+ efree (term);
105109 }
106110}
0 commit comments