Skip to content

Commit e26424d

Browse files
committed
Replace sass_strdup with sass_copy_c_string
1 parent a495d11 commit e26424d

File tree

9 files changed

+44
-55
lines changed

9 files changed

+44
-55
lines changed

src/context.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ namespace Sass {
289289
const char* contents = resources[idx].contents;
290290
// keep a copy of the path around (for parserstates)
291291
// ToDo: we clean it, but still not very elegant!?
292-
strings.push_back(sass_strdup(inc.abs_path.c_str()));
292+
strings.push_back(sass_copy_c_string(inc.abs_path.c_str()));
293293
// create the initial parser state from resource
294294
ParserState pstate(strings.back(), contents, idx);
295295

@@ -519,7 +519,7 @@ namespace Sass {
519519
}
520520
// create a copy of the resulting buffer string
521521
// this must be freed or taken over by implementor
522-
return sass_strdup(emitted.buffer.c_str());
522+
return sass_copy_c_string(emitted.buffer.c_str());
523523
}
524524

525525
void Context::apply_custom_headers(Block* root, const char* ctx_path, ParserState pstate)
@@ -606,7 +606,7 @@ namespace Sass {
606606

607607
// ToDo: this may be resolved via custom importers
608608
std::string abs_path(rel2abs(entry_path));
609-
char* abs_path_c_str = sass_strdup(abs_path.c_str());
609+
char* abs_path_c_str = sass_copy_c_string(abs_path.c_str());
610610
strings.push_back(abs_path_c_str);
611611

612612
// create entry only for the import stack
@@ -693,7 +693,7 @@ namespace Sass {
693693
if (source_map_file == "") return 0;
694694
char* result = 0;
695695
std::string map = emitter.render_srcmap(*this);
696-
result = sass_strdup(map.c_str());
696+
result = sass_copy_c_string(map.c_str());
697697
return result;
698698
}
699699

src/expand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ namespace Sass {
197197
{
198198
Expression* mq = m->media_queries()->perform(&eval);
199199
std::string str_mq(mq->to_string(ctx.c_options));
200-
char* str = sass_strdup(str_mq.c_str());
200+
char* str = sass_copy_c_string(str_mq.c_str());
201201
ctx.strings.push_back(str);
202202
Parser p(Parser::from_c_str(str, ctx, mq->pstate()));
203203
mq = p.parse_media_queries();

src/sass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ extern "C" {
3939
char* ADDCALL sass_string_quote (const char *str, const char quote_mark)
4040
{
4141
std::string quoted = quote(str, quote_mark);
42-
return sass_strdup(quoted.c_str());
42+
return sass_copy_c_string(quoted.c_str());
4343
}
4444

4545
// caller must free the returned memory
4646
char* ADDCALL sass_string_unquote (const char *str)
4747
{
4848
std::string unquoted = unquote(str);
49-
return sass_strdup(unquoted.c_str());
49+
return sass_copy_c_string(unquoted.c_str());
5050
}
5151

5252
// Make sure to free the returned value!
5353
// Incs array has to be null terminated!
5454
char* ADDCALL sass_resolve_file (const char* file, const char* paths[])
5555
{
5656
std::string resolved(File::find_file(file, paths));
57-
return sass_strdup(resolved.c_str());
57+
return sass_copy_c_string(resolved.c_str());
5858
}
5959

6060
// Get compiled libsass version

src/sass_context.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extern "C" {
2828
#define IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(type, option, def) \
2929
type ADDCALL sass_option_get_##option (struct Sass_Options* options) { return safe_str(options->option, def); } \
3030
void ADDCALL sass_option_set_##option (struct Sass_Options* options, type option) \
31-
{ free(options->option); options->option = option || def ? sass_strdup(option ? option : def) : 0; }
31+
{ free(options->option); options->option = option || def ? sass_copy_c_string(option ? option : def) : 0; }
3232

3333
#define IMPLEMENT_SASS_CONTEXT_GETTER(type, option) \
3434
type ADDCALL sass_context_get_##option (struct Sass_Context* ctx) { return ctx->option; }
@@ -106,10 +106,10 @@ extern "C" {
106106
json_append_member(json_err, "formatted", json_mkstring(msg_stream.str().c_str()));
107107

108108
c_ctx->error_json = json_stringify(json_err, " ");;
109-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
110-
c_ctx->error_text = sass_strdup(e.what());
109+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
110+
c_ctx->error_text = sass_copy_c_string(e.what());
111111
c_ctx->error_status = 1;
112-
c_ctx->error_file = sass_strdup(e.pstate.path);
112+
c_ctx->error_file = sass_copy_c_string(e.pstate.path);
113113
c_ctx->error_line = e.pstate.line+1;
114114
c_ctx->error_column = e.pstate.column+1;
115115
c_ctx->error_src = e.pstate.src;
@@ -125,8 +125,8 @@ extern "C" {
125125
json_append_member(json_err, "message", json_mkstring(ba.what()));
126126
json_append_member(json_err, "formatted", json_mkstring(msg_stream.str().c_str()));
127127
c_ctx->error_json = json_stringify(json_err, " ");;
128-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
129-
c_ctx->error_text = sass_strdup(ba.what());
128+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
129+
c_ctx->error_text = sass_copy_c_string(ba.what());
130130
c_ctx->error_status = 2;
131131
c_ctx->output_string = 0;
132132
c_ctx->source_map_string = 0;
@@ -140,8 +140,8 @@ extern "C" {
140140
json_append_member(json_err, "message", json_mkstring(e.what()));
141141
json_append_member(json_err, "formatted", json_mkstring(msg_stream.str().c_str()));
142142
c_ctx->error_json = json_stringify(json_err, " ");;
143-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
144-
c_ctx->error_text = sass_strdup(e.what());
143+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
144+
c_ctx->error_text = sass_copy_c_string(e.what());
145145
c_ctx->error_status = 3;
146146
c_ctx->output_string = 0;
147147
c_ctx->source_map_string = 0;
@@ -155,8 +155,8 @@ extern "C" {
155155
json_append_member(json_err, "message", json_mkstring(e.c_str()));
156156
json_append_member(json_err, "formatted", json_mkstring(msg_stream.str().c_str()));
157157
c_ctx->error_json = json_stringify(json_err, " ");;
158-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
159-
c_ctx->error_text = sass_strdup(e.c_str());
158+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
159+
c_ctx->error_text = sass_copy_c_string(e.c_str());
160160
c_ctx->error_status = 4;
161161
c_ctx->output_string = 0;
162162
c_ctx->source_map_string = 0;
@@ -170,8 +170,8 @@ extern "C" {
170170
json_append_member(json_err, "message", json_mkstring(e));
171171
json_append_member(json_err, "formatted", json_mkstring(msg_stream.str().c_str()));
172172
c_ctx->error_json = json_stringify(json_err, " ");;
173-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
174-
c_ctx->error_text = sass_strdup(e);
173+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
174+
c_ctx->error_text = sass_copy_c_string(e);
175175
c_ctx->error_status = 4;
176176
c_ctx->output_string = 0;
177177
c_ctx->source_map_string = 0;
@@ -184,8 +184,8 @@ extern "C" {
184184
json_append_member(json_err, "status", json_mknumber(5));
185185
json_append_member(json_err, "message", json_mkstring("unknown"));
186186
c_ctx->error_json = json_stringify(json_err, " ");;
187-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
188-
c_ctx->error_text = sass_strdup("unknown");
187+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
188+
c_ctx->error_text = sass_copy_c_string("unknown");
189189
c_ctx->error_status = 5;
190190
c_ctx->output_string = 0;
191191
c_ctx->source_map_string = 0;
@@ -565,7 +565,7 @@ extern "C" {
565565
static void sass_clear_context (struct Sass_Context* ctx)
566566
{
567567
if (ctx == 0) return;
568-
// release the allocated memory (mostly via sass_strdup)
568+
// release the allocated memory (mostly via sass_copy_c_string)
569569
if (ctx->output_string) free(ctx->output_string);
570570
if (ctx->source_map_string) free(ctx->source_map_string);
571571
if (ctx->error_message) free(ctx->error_message);
@@ -695,7 +695,7 @@ extern "C" {
695695

696696
struct string_list* include_path = (struct string_list*) calloc(1, sizeof(struct string_list));
697697
if (include_path == 0) return;
698-
include_path->string = path ? sass_strdup(path) : 0;
698+
include_path->string = path ? sass_copy_c_string(path) : 0;
699699
struct string_list* last = options->include_paths;
700700
if (!options->include_paths) {
701701
options->include_paths = include_path;
@@ -713,7 +713,7 @@ extern "C" {
713713

714714
struct string_list* plugin_path = (struct string_list*) calloc(1, sizeof(struct string_list));
715715
if (plugin_path == 0) return;
716-
plugin_path->string = path ? sass_strdup(path) : 0;
716+
plugin_path->string = path ? sass_copy_c_string(path) : 0;
717717
struct string_list* last = options->plugin_paths;
718718
if (!options->plugin_paths) {
719719
options->plugin_paths = plugin_path;

src/sass_functions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ extern "C" {
7272
{
7373
Sass_Import* v = (Sass_Import*) calloc(1, sizeof(Sass_Import));
7474
if (v == 0) return 0;
75-
v->imp_path = imp_path ? sass_strdup(imp_path) : 0;
76-
v->abs_path = abs_path ? sass_strdup(abs_path) : 0;
75+
v->imp_path = imp_path ? sass_copy_c_string(imp_path) : 0;
76+
v->abs_path = abs_path ? sass_copy_c_string(abs_path) : 0;
7777
v->source = source;
7878
v->srcmap = srcmap;
7979
v->error = 0;
@@ -93,7 +93,7 @@ extern "C" {
9393
{
9494
if (import == 0) return 0;
9595
if (import->error) free(import->error);
96-
import->error = error ? sass_strdup(error) : 0;
96+
import->error = error ? sass_copy_c_string(error) : 0;
9797
import->line = line ? line : -1;
9898
import->column = col ? col : -1;
9999
return import;

src/sass_interface.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,31 @@ extern "C" {
9191
catch (Exception::InvalidSass& e) {
9292
std::stringstream msg_stream;
9393
msg_stream << e.pstate.path << ":" << e.pstate.line << ": " << e.what() << std::endl;
94-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
94+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
9595
c_ctx->error_status = 1;
9696
c_ctx->output_string = 0;
9797
c_ctx->source_map_string = 0;
9898
}
9999
catch(std::bad_alloc& ba) {
100100
std::stringstream msg_stream;
101101
msg_stream << "Unable to allocate memory: " << ba.what() << std::endl;
102-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
102+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
103103
c_ctx->error_status = 1;
104104
c_ctx->output_string = 0;
105105
c_ctx->source_map_string = 0;
106106
}
107107
catch (std::exception& e) {
108108
std::stringstream msg_stream;
109109
msg_stream << "Error: " << e.what() << std::endl;
110-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
110+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
111111
c_ctx->error_status = 1;
112112
c_ctx->output_string = 0;
113113
c_ctx->source_map_string = 0;
114114
}
115115
catch (std::string& e) {
116116
std::stringstream msg_stream;
117117
msg_stream << "Error: " << e << std::endl;
118-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
118+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
119119
c_ctx->error_status = 1;
120120
c_ctx->output_string = 0;
121121
c_ctx->source_map_string = 0;
@@ -124,7 +124,7 @@ extern "C" {
124124
// couldn't find the specified file in the include paths; report an error
125125
std::stringstream msg_stream;
126126
msg_stream << "Unknown error occurred" << std::endl;
127-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
127+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
128128
c_ctx->error_status = 1;
129129
c_ctx->output_string = 0;
130130
c_ctx->source_map_string = 0;
@@ -166,31 +166,31 @@ extern "C" {
166166
catch (Exception::InvalidSass& e) {
167167
std::stringstream msg_stream;
168168
msg_stream << e.pstate.path << ":" << e.pstate.line << ": " << e.what() << std::endl;
169-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
169+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
170170
c_ctx->error_status = 1;
171171
c_ctx->output_string = 0;
172172
c_ctx->source_map_string = 0;
173173
}
174174
catch(std::bad_alloc& ba) {
175175
std::stringstream msg_stream;
176176
msg_stream << "Unable to allocate memory: " << ba.what() << std::endl;
177-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
177+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
178178
c_ctx->error_status = 1;
179179
c_ctx->output_string = 0;
180180
c_ctx->source_map_string = 0;
181181
}
182182
catch (std::exception& e) {
183183
std::stringstream msg_stream;
184184
msg_stream << "Error: " << e.what() << std::endl;
185-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
185+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
186186
c_ctx->error_status = 1;
187187
c_ctx->output_string = 0;
188188
c_ctx->source_map_string = 0;
189189
}
190190
catch (std::string& e) {
191191
std::stringstream msg_stream;
192192
msg_stream << "Error: " << e << std::endl;
193-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
193+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
194194
c_ctx->error_status = 1;
195195
c_ctx->output_string = 0;
196196
c_ctx->source_map_string = 0;
@@ -199,7 +199,7 @@ extern "C" {
199199
// couldn't find the specified file in the include paths; report an error
200200
std::stringstream msg_stream;
201201
msg_stream << "Unknown error occurred" << std::endl;
202-
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
202+
c_ctx->error_message = sass_copy_c_string(msg_stream.str().c_str());
203203
c_ctx->error_status = 1;
204204
c_ctx->output_string = 0;
205205
c_ctx->source_map_string = 0;

src/sass_values.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extern "C" {
9191
if (v == 0) return 0;
9292
v->number.tag = SASS_NUMBER;
9393
v->number.value = val;
94-
v->number.unit = unit ? sass_strdup(unit) : 0;
94+
v->number.unit = unit ? sass_copy_c_string(unit) : 0;
9595
if (v->number.unit == 0) { free(v); return 0; }
9696
return v;
9797
}
@@ -114,7 +114,7 @@ extern "C" {
114114
if (v == 0) return 0;
115115
v->string.quoted = false;
116116
v->string.tag = SASS_STRING;
117-
v->string.value = val ? sass_strdup(val) : 0;
117+
v->string.value = val ? sass_copy_c_string(val) : 0;
118118
if (v->string.value == 0) { free(v); return 0; }
119119
return v;
120120
}
@@ -125,7 +125,7 @@ extern "C" {
125125
if (v == 0) return 0;
126126
v->string.quoted = true;
127127
v->string.tag = SASS_STRING;
128-
v->string.value = val ? sass_strdup(val) : 0;
128+
v->string.value = val ? sass_copy_c_string(val) : 0;
129129
if (v->string.value == 0) { free(v); return 0; }
130130
return v;
131131
}
@@ -166,7 +166,7 @@ extern "C" {
166166
union Sass_Value* v = (Sass_Value*) calloc(1, sizeof(Sass_Value));
167167
if (v == 0) return 0;
168168
v->error.tag = SASS_ERROR;
169-
v->error.message = msg ? sass_strdup(msg) : 0;
169+
v->error.message = msg ? sass_copy_c_string(msg) : 0;
170170
if (v->error.message == 0) { free(v); return 0; }
171171
return v;
172172
}
@@ -176,7 +176,7 @@ extern "C" {
176176
union Sass_Value* v = (Sass_Value*) calloc(1, sizeof(Sass_Value));
177177
if (v == 0) return 0;
178178
v->warning.tag = SASS_WARNING;
179-
v->warning.message = msg ? sass_strdup(msg) : 0;
179+
v->warning.message = msg ? sass_copy_c_string(msg) : 0;
180180
if (v->warning.message == 0) { free(v); return 0; }
181181
return v;
182182
}

src/util.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ namespace Sass {
2323
return ::round(val);
2424
}
2525

26-
/* Sadly, sass_strdup is not portable. */
27-
char *sass_strdup(const char *str)
28-
{
29-
char *ret = (char*) malloc(strlen(str) + 1);
30-
if (ret == NULL)
31-
out_of_memory();
32-
strcpy(ret, str);
33-
return ret;
34-
}
35-
3626
/* Locale unspecific atof function. */
3727
double sass_atof(const char *str)
3828
{
@@ -45,7 +35,7 @@ namespace Sass {
4535
if(found != NULL){
4636
// substitution is required. perform the substitution on a copy
4737
// of the string. This is slower but it is thread safe.
48-
char *copy = sass_strdup(str);
38+
char *copy = sass_copy_c_string(str);
4939
*(copy + (found - str)) = separator;
5040
double res = atof(copy);
5141
free(copy);

src/util.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace Sass {
1818
} while (0)
1919

2020
double round(double val, size_t precision = 0);
21-
char* sass_strdup(const char* str);
2221
double sass_atof(const char* str);
2322
const char* safe_str(const char *, const char* = "");
2423
void free_string_array(char **);

0 commit comments

Comments
 (0)