Skip to content

Commit

Permalink
Merge pull request #1521 from saper/nocexcept
Browse files Browse the repository at this point in the history
VS Exception handling only in C++ code
  • Loading branch information
xzyfer committed Sep 6, 2015
2 parents 6075ec0 + 946b24e commit 3ade3d0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 76 deletions.
38 changes: 3 additions & 35 deletions src/sass_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,40 +182,6 @@ extern "C" {
type sass_context_take_##option (struct Sass_Context* ctx) \
{ type foo = ctx->option; ctx->option = 0; return foo; }

// helper for safe access to c_ctx
static const char* safe_str (const char* str) {
return str == NULL ? "" : str;
}

static void copy_strings(const std::vector<std::string>& strings, char*** array) {
int num = static_cast<int>(strings.size());
char** arr = (char**) malloc(sizeof(char*) * (num + 1));
if (arr == 0) throw(std::bad_alloc());

for(int i = 0; i < num; i++) {
arr[i] = (char*) malloc(sizeof(char) * (strings[i].size() + 1));
if (arr[i] == 0) throw(std::bad_alloc());
std::copy(strings[i].begin(), strings[i].end(), arr[i]);
arr[i][strings[i].size()] = '\0';
}

arr[num] = 0;
*array = arr;
}

static void free_string_array(char ** arr) {
if(!arr)
return;

char **it = arr;
while (it && (*it)) {
free(*it);
++it;
}

free(arr);
}

static int handle_errors(Sass_Context* c_ctx) {
try {
throw;
Expand Down Expand Up @@ -528,7 +494,9 @@ extern "C" {
size_t headers = cpp_ctx->head_imports;

// copy the included files on to the context (dont forget to free)
if (root) copy_strings(cpp_ctx->get_included_files(skip, headers), &c_ctx->included_files);
if (root)
if (copy_strings(cpp_ctx->get_included_files(skip, headers), &c_ctx->included_files) == NULL)
throw(std::bad_alloc());

// return parsed block
return root;
Expand Down
44 changes: 7 additions & 37 deletions src/sass_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,14 @@ extern "C" {
sass_context* sass_new_context()
{ return (sass_context*) calloc(1, sizeof(sass_context)); }

// helper for safe access to c_ctx
static const char* safe_str (const char* str) {
return str == NULL ? "" : str;
}

static void copy_strings(const std::vector<std::string>& strings, char*** array, int skip = 0) {
int num = static_cast<int>(strings.size());
char** arr = (char**) malloc(sizeof(char*) * (num + 1));

for(int i = skip; i < num; i++) {
arr[i-skip] = (char*) malloc(sizeof(char) * (strings[i].size() + 1));
std::copy(strings[i].begin(), strings[i].end(), arr[i-skip]);
arr[i-skip][strings[i].size()] = '\0';
}

arr[num-skip] = 0;
*array = arr;
}

static void free_string_array(char ** arr) {
if(!arr)
return;

char **it = arr;
while (it && (*it)) {
free(*it);
++it;
}

free(arr);
}

void sass_free_context(sass_context* ctx)
{
if (ctx->output_string) free(ctx->output_string);
if (ctx->source_map_string) free(ctx->source_map_string);
if (ctx->error_message) free(ctx->error_message);
if (ctx->c_functions) free(ctx->c_functions);

free_string_array(ctx->included_files);
Sass::free_string_array(ctx->included_files);

free(ctx);
}
Expand All @@ -73,7 +41,7 @@ extern "C" {
if (ctx->error_message) free(ctx->error_message);
if (ctx->c_functions) free(ctx->c_functions);

free_string_array(ctx->included_files);
Sass::free_string_array(ctx->included_files);

free(ctx);
}
Expand All @@ -83,7 +51,7 @@ extern "C" {

void sass_free_folder_context(sass_folder_context* ctx)
{
free_string_array(ctx->included_files);
Sass::free_string_array(ctx->included_files);
free(ctx);
}

Expand Down Expand Up @@ -135,7 +103,8 @@ extern "C" {
c_ctx->error_message = 0;
c_ctx->error_status = 0;

copy_strings(cpp_ctx.get_included_files(true), &c_ctx->included_files, 1);
if (copy_strings(cpp_ctx.get_included_files(true), &c_ctx->included_files, 1) == NULL)
throw(std::bad_alloc());
}
catch (Error_Invalid& e) {
std::stringstream msg_stream;
Expand Down Expand Up @@ -227,7 +196,8 @@ extern "C" {
c_ctx->error_message = 0;
c_ctx->error_status = 0;

copy_strings(cpp_ctx.get_included_files(false), &c_ctx->included_files);
if (copy_strings(cpp_ctx.get_included_files(false), &c_ctx->included_files) == NULL)
throw(std::bad_alloc());
}
catch (Error_Invalid& e) {
std::stringstream msg_stream;
Expand Down
38 changes: 38 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ namespace Sass {
return atof(str);
}

// helper for safe access to c_ctx
const char* safe_str (const char* str) {
return str == NULL ? "" : str;
}

void free_string_array(char ** arr) {
if(!arr)
return;

char **it = arr;
while (it && (*it)) {
free(*it);
++it;
}

free(arr);
}

char **copy_strings(const std::vector<std::string>& strings, char*** array, int skip) {
int num = static_cast<int>(strings.size()) - skip;
char** arr = (char**) calloc(num + 1, sizeof(char*));
if (arr == 0)
return *array = (char **)NULL;

for(int i = 0; i < num; i++) {
arr[i] = (char*) malloc(sizeof(char) * (strings[i + skip].size() + 1));
if (arr[i] == 0) {
free_string_array(arr);
return *array = (char **)NULL;
}
std::copy(strings[i + skip].begin(), strings[i + skip].end(), arr[i]);
arr[i][strings[i + skip].size()] = '\0';
}

arr[num] = 0;
return *array = arr;
}

std::string string_eval_escapes(const std::string& s)
{

Expand Down
3 changes: 3 additions & 0 deletions src/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Sass {

char* sass_strdup(const char* str);
double sass_atof(const char* str);
const char* safe_str(const char *);
void free_string_array(char **);
char **copy_strings(const std::vector<std::string>&, char ***, int = 0);
std::string string_escape(const std::string& str);
std::string string_unescape(const std::string& str);
std::string string_eval_escapes(const std::string& str);
Expand Down
4 changes: 0 additions & 4 deletions win/libsass.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ExceptionHandling>SyncCThrow</ExceptionHandling>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -140,7 +139,6 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ExceptionHandling>SyncCThrow</ExceptionHandling>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -156,7 +154,6 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ExceptionHandling>SyncCThrow</ExceptionHandling>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -174,7 +171,6 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ExceptionHandling>SyncCThrow</ExceptionHandling>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down

0 comments on commit 3ade3d0

Please sign in to comment.