Skip to content

Commit b3ff547

Browse files
committed
Merge pull request sass#781 from mgreter/fix/hardened-api
Improve and harden handling of edge cases in C-API
2 parents 5483186 + 33965ce commit b3ff547

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

context.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ namespace Sass {
270270
if (i == 0) root = ast;
271271
style_sheets[queue[i].load_path] = ast;
272272
}
273+
if (root == 0) return 0;
273274
Env tge;
274275
Backtrace backtrace(0, "", Position(), "");
275276
register_built_in_functions(*this, &tge);

sass_context.cpp

+37-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <unistd.h>
55
#endif
66

7+
#include <stdexcept>
78
#include "json.hpp"
89
#include "context.hpp"
910
#include "sass_values.h"
@@ -437,25 +438,38 @@ extern "C" {
437438
Sass_File_Context* ADDCALL sass_make_file_context(const char* input_path)
438439
{
439440
struct Sass_File_Context* ctx = (struct Sass_File_Context*) calloc(1, sizeof(struct Sass_File_Context));
440-
if (ctx == 0) return 0;
441+
if (ctx == 0) { cerr << "Error allocating memory for file context" << endl; return 0; }
441442
ctx->type = SASS_CONTEXT_FILE;
442-
sass_option_set_input_path(ctx, input_path);
443+
try {
444+
if (input_path == 0) { throw(runtime_error("File context created without an input path")); }
445+
if (*input_path == 0) { throw(runtime_error("File context created with empty input path")); }
446+
sass_option_set_input_path(ctx, input_path);
447+
} catch (...) {
448+
handle_errors(ctx);
449+
}
443450
return ctx;
444451
}
445452

446453
Sass_Data_Context* ADDCALL sass_make_data_context(char* source_string)
447454
{
448455
struct Sass_Data_Context* ctx = (struct Sass_Data_Context*) calloc(1, sizeof(struct Sass_Data_Context));
449-
if (ctx == 0) return 0;
456+
if (ctx == 0) { cerr << "Error allocating memory for data context" << endl; return 0; }
450457
ctx->type = SASS_CONTEXT_DATA;
451-
ctx->source_string = source_string;
458+
try {
459+
if (source_string == 0) { throw(runtime_error("Data context created without a source string")); }
460+
if (*source_string == 0) { throw(runtime_error("Data context created with empty source string")); }
461+
ctx->source_string = source_string;
462+
} catch (...) {
463+
handle_errors(ctx);
464+
}
452465
return ctx;
453466
}
454467

455468
struct Sass_Compiler* ADDCALL sass_make_file_compiler (struct Sass_File_Context* c_ctx)
456469
{
470+
if (c_ctx == 0) return 0;
457471
struct Sass_Compiler* compiler = (struct Sass_Compiler*) calloc(1, sizeof(struct Sass_Compiler));
458-
if (compiler == 0) return 0;
472+
if (compiler == 0) { cerr << "Error allocating memory for file compiler" << endl; return 0; }
459473
compiler->state = SASS_COMPILER_CREATED;
460474
compiler->c_ctx = c_ctx;
461475
Context::Data cpp_opt = Context::Data();
@@ -466,8 +480,9 @@ extern "C" {
466480

467481
struct Sass_Compiler* ADDCALL sass_make_data_compiler (struct Sass_Data_Context* c_ctx)
468482
{
483+
if (c_ctx == 0) return 0;
469484
struct Sass_Compiler* compiler = (struct Sass_Compiler*) calloc(1, sizeof(struct Sass_Compiler));
470-
if (compiler == 0) return 0;
485+
if (compiler == 0) { cerr << "Error allocating memory for data compiler" << endl; return 0; }
471486
compiler->state = SASS_COMPILER_CREATED;
472487
compiler->c_ctx = c_ctx;
473488
Context::Data cpp_opt = Context::Data();
@@ -478,26 +493,37 @@ extern "C" {
478493

479494
int ADDCALL sass_compile_data_context(Sass_Data_Context* data_ctx)
480495
{
496+
if (data_ctx == 0) return 1;
481497
Sass_Context* c_ctx = data_ctx;
498+
if (c_ctx->error_status)
499+
return c_ctx->error_status;
482500
Context::Data cpp_opt = Context::Data();
483-
cpp_opt.source_c_str(data_ctx->source_string);
501+
try { cpp_opt.source_c_str(data_ctx->source_string); }
502+
catch (...) { return handle_errors(c_ctx) || 1; }
484503
return sass_compile_context(c_ctx, cpp_opt);
485504
}
486505

487506
int ADDCALL sass_compile_file_context(Sass_File_Context* file_ctx)
488507
{
508+
if (file_ctx == 0) return 1;
489509
Sass_Context* c_ctx = file_ctx;
510+
if (c_ctx->error_status)
511+
return c_ctx->error_status;
490512
Context::Data cpp_opt = Context::Data();
491-
cpp_opt.entry_point(file_ctx->input_path);
513+
try { cpp_opt.entry_point(file_ctx->input_path); }
514+
catch (...) { return handle_errors(c_ctx) || 1; }
492515
return sass_compile_context(c_ctx, cpp_opt);
493516
}
494517

495518
int ADDCALL sass_compiler_parse(struct Sass_Compiler* compiler)
496519
{
520+
if (compiler == 0) return 1;
497521
if (compiler->state == SASS_COMPILER_PARSED) return 0;
498522
if (compiler->state != SASS_COMPILER_CREATED) return -1;
499523
if (compiler->c_ctx == NULL) return 1;
500524
if (compiler->cpp_ctx == NULL) return 1;
525+
if (compiler->c_ctx->error_status)
526+
return compiler->c_ctx->error_status;
501527
compiler->state = SASS_COMPILER_PARSED;
502528
Context* cpp_ctx = (Context*) compiler->cpp_ctx;
503529
// parse the context we have set up (file or data)
@@ -508,12 +534,14 @@ extern "C" {
508534

509535
int ADDCALL sass_compiler_execute(struct Sass_Compiler* compiler)
510536
{
511-
if (compiler == 0) return 0;
537+
if (compiler == 0) return 1;
512538
if (compiler->state == SASS_COMPILER_EXECUTED) return 0;
513539
if (compiler->state != SASS_COMPILER_PARSED) return -1;
514540
if (compiler->c_ctx == NULL) return 1;
515541
if (compiler->cpp_ctx == NULL) return 1;
516542
if (compiler->root == NULL) return 1;
543+
if (compiler->c_ctx->error_status)
544+
return compiler->c_ctx->error_status;
517545
compiler->state = SASS_COMPILER_EXECUTED;
518546
Context* cpp_ctx = (Context*) compiler->cpp_ctx;
519547
Block* root = (Block*) compiler->root;

0 commit comments

Comments
 (0)