Skip to content

Commit 9fcea20

Browse files
committed
VS Exception handling only in C++ code
Catch exceptions only in C++ code, extern "C" functions should be exception-free. Fixes: #1508
1 parent e1085b6 commit 9fcea20

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

src/sass_context.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,6 @@ extern "C" {
187187
return str == NULL ? "" : str;
188188
}
189189

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-
206190
static void free_string_array(char ** arr) {
207191
if(!arr)
208192
return;
@@ -216,6 +200,26 @@ extern "C" {
216200
free(arr);
217201
}
218202

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+
219223
static int handle_errors(Sass_Context* c_ctx) {
220224
try {
221225
throw;
@@ -528,7 +532,9 @@ extern "C" {
528532
size_t headers = cpp_ctx->head_imports;
529533

530534
// 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());
532538

533539
// return parsed block
534540
return root;

win/libsass.vcxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
<WarningLevel>Level3</WarningLevel>
127127
<Optimization>Disabled</Optimization>
128128
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
129-
<ExceptionHandling>SyncCThrow</ExceptionHandling>
129+
<ExceptionHandling>Sync</ExceptionHandling>
130130
</ClCompile>
131131
<Link>
132132
<SubSystem>Console</SubSystem>
@@ -140,7 +140,7 @@
140140
<WarningLevel>Level3</WarningLevel>
141141
<Optimization>Disabled</Optimization>
142142
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
143-
<ExceptionHandling>SyncCThrow</ExceptionHandling>
143+
<ExceptionHandling>Sync</ExceptionHandling>
144144
</ClCompile>
145145
<Link>
146146
<SubSystem>Console</SubSystem>
@@ -156,7 +156,7 @@
156156
<FunctionLevelLinking>true</FunctionLevelLinking>
157157
<IntrinsicFunctions>true</IntrinsicFunctions>
158158
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
159-
<ExceptionHandling>SyncCThrow</ExceptionHandling>
159+
<ExceptionHandling>Sync</ExceptionHandling>
160160
</ClCompile>
161161
<Link>
162162
<SubSystem>Console</SubSystem>
@@ -174,7 +174,7 @@
174174
<FunctionLevelLinking>true</FunctionLevelLinking>
175175
<IntrinsicFunctions>true</IntrinsicFunctions>
176176
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
177-
<ExceptionHandling>SyncCThrow</ExceptionHandling>
177+
<ExceptionHandling>Sync</ExceptionHandling>
178178
</ClCompile>
179179
<Link>
180180
<SubSystem>Console</SubSystem>

0 commit comments

Comments
 (0)