@@ -187,22 +187,6 @@ extern "C" {
187
187
return str == NULL ? " " : str;
188
188
}
189
189
190
- static void copy_strings (const std::vector<std::string>& strings, char *** array) {
191
- int num = static_cast <int >(strings.size ());
192
- char ** arr = (char **) malloc (sizeof (char *) * (num + 1 ));
193
- if (arr == 0 ) throw (std::bad_alloc ());
194
-
195
- for (int i = 0 ; i < num; i++) {
196
- arr[i] = (char *) malloc (sizeof (char ) * (strings[i].size () + 1 ));
197
- if (arr[i] == 0 ) throw (std::bad_alloc ());
198
- std::copy (strings[i].begin (), strings[i].end (), arr[i]);
199
- arr[i][strings[i].size ()] = ' \0 ' ;
200
- }
201
-
202
- arr[num] = 0 ;
203
- *array = arr;
204
- }
205
-
206
190
static void free_string_array (char ** arr) {
207
191
if (!arr)
208
192
return ;
@@ -216,6 +200,26 @@ extern "C" {
216
200
free (arr);
217
201
}
218
202
203
+ static char **copy_strings (const std::vector<std::string>& strings, char *** array) {
204
+ int num = static_cast <int >(strings.size ());
205
+ char ** arr = (char **) calloc (num + 1 , sizeof (char *));
206
+ if (arr == 0 )
207
+ return *array = (char **)NULL ;
208
+
209
+ for (int i = 0 ; i < num; i++) {
210
+ arr[i] = (char *) malloc (sizeof (char ) * (strings[i].size () + 1 ));
211
+ if (arr[i] == 0 ) {
212
+ free_string_array (arr);
213
+ return *array = (char **)NULL ;
214
+ }
215
+ std::copy (strings[i].begin (), strings[i].end (), arr[i]);
216
+ arr[i][strings[i].size ()] = ' \0 ' ;
217
+ }
218
+
219
+ arr[num] = 0 ;
220
+ return *array = arr;
221
+ }
222
+
219
223
static int handle_errors (Sass_Context* c_ctx) {
220
224
try {
221
225
throw ;
@@ -528,7 +532,9 @@ extern "C" {
528
532
size_t headers = cpp_ctx->head_imports ;
529
533
530
534
// copy the included files on to the context (dont forget to free)
531
- if (root) copy_strings (cpp_ctx->get_included_files (skip, headers), &c_ctx->included_files );
535
+ if (root)
536
+ if (copy_strings (cpp_ctx->get_included_files (skip, headers), &c_ctx->included_files ) == NULL )
537
+ throw (std::bad_alloc ());
532
538
533
539
// return parsed block
534
540
return root;
0 commit comments