Skip to content

Commit 3ade3d0

Browse files
committed
Merge pull request #1521 from saper/nocexcept
VS Exception handling only in C++ code
2 parents 6075ec0 + 946b24e commit 3ade3d0

File tree

5 files changed

+51
-76
lines changed

5 files changed

+51
-76
lines changed

src/sass_context.cpp

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -182,40 +182,6 @@ extern "C" {
182182
type sass_context_take_##option (struct Sass_Context* ctx) \
183183
{ type foo = ctx->option; ctx->option = 0; return foo; }
184184

185-
// helper for safe access to c_ctx
186-
static const char* safe_str (const char* str) {
187-
return str == NULL ? "" : str;
188-
}
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-
static void free_string_array(char ** arr) {
207-
if(!arr)
208-
return;
209-
210-
char **it = arr;
211-
while (it && (*it)) {
212-
free(*it);
213-
++it;
214-
}
215-
216-
free(arr);
217-
}
218-
219185
static int handle_errors(Sass_Context* c_ctx) {
220186
try {
221187
throw;
@@ -528,7 +494,9 @@ extern "C" {
528494
size_t headers = cpp_ctx->head_imports;
529495

530496
// 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);
497+
if (root)
498+
if (copy_strings(cpp_ctx->get_included_files(skip, headers), &c_ctx->included_files) == NULL)
499+
throw(std::bad_alloc());
532500

533501
// return parsed block
534502
return root;

src/sass_interface.cpp

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,14 @@ extern "C" {
1919
sass_context* sass_new_context()
2020
{ return (sass_context*) calloc(1, sizeof(sass_context)); }
2121

22-
// helper for safe access to c_ctx
23-
static const char* safe_str (const char* str) {
24-
return str == NULL ? "" : str;
25-
}
26-
27-
static void copy_strings(const std::vector<std::string>& strings, char*** array, int skip = 0) {
28-
int num = static_cast<int>(strings.size());
29-
char** arr = (char**) malloc(sizeof(char*) * (num + 1));
30-
31-
for(int i = skip; i < num; i++) {
32-
arr[i-skip] = (char*) malloc(sizeof(char) * (strings[i].size() + 1));
33-
std::copy(strings[i].begin(), strings[i].end(), arr[i-skip]);
34-
arr[i-skip][strings[i].size()] = '\0';
35-
}
36-
37-
arr[num-skip] = 0;
38-
*array = arr;
39-
}
40-
41-
static void free_string_array(char ** arr) {
42-
if(!arr)
43-
return;
44-
45-
char **it = arr;
46-
while (it && (*it)) {
47-
free(*it);
48-
++it;
49-
}
50-
51-
free(arr);
52-
}
53-
5422
void sass_free_context(sass_context* ctx)
5523
{
5624
if (ctx->output_string) free(ctx->output_string);
5725
if (ctx->source_map_string) free(ctx->source_map_string);
5826
if (ctx->error_message) free(ctx->error_message);
5927
if (ctx->c_functions) free(ctx->c_functions);
6028

61-
free_string_array(ctx->included_files);
29+
Sass::free_string_array(ctx->included_files);
6230

6331
free(ctx);
6432
}
@@ -73,7 +41,7 @@ extern "C" {
7341
if (ctx->error_message) free(ctx->error_message);
7442
if (ctx->c_functions) free(ctx->c_functions);
7543

76-
free_string_array(ctx->included_files);
44+
Sass::free_string_array(ctx->included_files);
7745

7846
free(ctx);
7947
}
@@ -83,7 +51,7 @@ extern "C" {
8351

8452
void sass_free_folder_context(sass_folder_context* ctx)
8553
{
86-
free_string_array(ctx->included_files);
54+
Sass::free_string_array(ctx->included_files);
8755
free(ctx);
8856
}
8957

@@ -135,7 +103,8 @@ extern "C" {
135103
c_ctx->error_message = 0;
136104
c_ctx->error_status = 0;
137105

138-
copy_strings(cpp_ctx.get_included_files(true), &c_ctx->included_files, 1);
106+
if (copy_strings(cpp_ctx.get_included_files(true), &c_ctx->included_files, 1) == NULL)
107+
throw(std::bad_alloc());
139108
}
140109
catch (Error_Invalid& e) {
141110
std::stringstream msg_stream;
@@ -227,7 +196,8 @@ extern "C" {
227196
c_ctx->error_message = 0;
228197
c_ctx->error_status = 0;
229198

230-
copy_strings(cpp_ctx.get_included_files(false), &c_ctx->included_files);
199+
if (copy_strings(cpp_ctx.get_included_files(false), &c_ctx->included_files) == NULL)
200+
throw(std::bad_alloc());
231201
}
232202
catch (Error_Invalid& e) {
233203
std::stringstream msg_stream;

src/util.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,44 @@ namespace Sass {
4848
return atof(str);
4949
}
5050

51+
// helper for safe access to c_ctx
52+
const char* safe_str (const char* str) {
53+
return str == NULL ? "" : str;
54+
}
55+
56+
void free_string_array(char ** arr) {
57+
if(!arr)
58+
return;
59+
60+
char **it = arr;
61+
while (it && (*it)) {
62+
free(*it);
63+
++it;
64+
}
65+
66+
free(arr);
67+
}
68+
69+
char **copy_strings(const std::vector<std::string>& strings, char*** array, int skip) {
70+
int num = static_cast<int>(strings.size()) - skip;
71+
char** arr = (char**) calloc(num + 1, sizeof(char*));
72+
if (arr == 0)
73+
return *array = (char **)NULL;
74+
75+
for(int i = 0; i < num; i++) {
76+
arr[i] = (char*) malloc(sizeof(char) * (strings[i + skip].size() + 1));
77+
if (arr[i] == 0) {
78+
free_string_array(arr);
79+
return *array = (char **)NULL;
80+
}
81+
std::copy(strings[i + skip].begin(), strings[i + skip].end(), arr[i]);
82+
arr[i][strings[i + skip].size()] = '\0';
83+
}
84+
85+
arr[num] = 0;
86+
return *array = arr;
87+
}
88+
5189
std::string string_eval_escapes(const std::string& s)
5290
{
5391

src/util.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace Sass {
1212

1313
char* sass_strdup(const char* str);
1414
double sass_atof(const char* str);
15+
const char* safe_str(const char *);
16+
void free_string_array(char **);
17+
char **copy_strings(const std::vector<std::string>&, char ***, int = 0);
1518
std::string string_escape(const std::string& str);
1619
std::string string_unescape(const std::string& str);
1720
std::string string_eval_escapes(const std::string& str);

win/libsass.vcxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@
126126
<WarningLevel>Level3</WarningLevel>
127127
<Optimization>Disabled</Optimization>
128128
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
129-
<ExceptionHandling>SyncCThrow</ExceptionHandling>
130129
</ClCompile>
131130
<Link>
132131
<SubSystem>Console</SubSystem>
@@ -140,7 +139,6 @@
140139
<WarningLevel>Level3</WarningLevel>
141140
<Optimization>Disabled</Optimization>
142141
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
143-
<ExceptionHandling>SyncCThrow</ExceptionHandling>
144142
</ClCompile>
145143
<Link>
146144
<SubSystem>Console</SubSystem>
@@ -156,7 +154,6 @@
156154
<FunctionLevelLinking>true</FunctionLevelLinking>
157155
<IntrinsicFunctions>true</IntrinsicFunctions>
158156
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
159-
<ExceptionHandling>SyncCThrow</ExceptionHandling>
160157
</ClCompile>
161158
<Link>
162159
<SubSystem>Console</SubSystem>
@@ -174,7 +171,6 @@
174171
<FunctionLevelLinking>true</FunctionLevelLinking>
175172
<IntrinsicFunctions>true</IntrinsicFunctions>
176173
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
177-
<ExceptionHandling>SyncCThrow</ExceptionHandling>
178174
</ClCompile>
179175
<Link>
180176
<SubSystem>Console</SubSystem>

0 commit comments

Comments
 (0)