Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 540d1c6

Browse files
committed
Check file existence first
1 parent 39b6a2d commit 540d1c6

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ Version 0.1.1
8282

8383
To be released.
8484

85+
- Fixed segmentation fault for reading ``filename`` which does not exist.
86+
Now it raises a proper ``exceptions.IOError`` exception.
87+
8588

8689
Version 0.1.0
8790
'''''''''''''

sass.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <unistd.h>
12
#include <Python.h>
23
#include "sass_interface.h"
34

@@ -21,7 +22,7 @@ PySass_compile(PyObject *self, PyObject *args, PyObject *kwds)
2122
*output_style, *include_paths, *image_path,
2223
*result, *item;
2324
int expected_kwds, output_style_v;
24-
char *include_paths_v, *image_path_v, *item_buffer;
25+
char *filename_v, *include_paths_v, *image_path_v, *item_buffer;
2526
Py_ssize_t include_paths_num, include_paths_size, i, offset, item_size;
2627
union {
2728
struct sass_context *string;
@@ -184,8 +185,19 @@ PySass_compile(PyObject *self, PyObject *args, PyObject *kwds)
184185
result = NULL;
185186
goto finalize;
186187
}
188+
189+
filename_v = PyString_AsString(filename);
190+
191+
if (access(filename_v, R_OK) < 0) {
192+
PyErr_Format(PyExc_IOError,
193+
"filename '%s' cannot be read",
194+
filename_v);
195+
result = NULL;
196+
goto finalize;
197+
}
198+
187199
context.filename = sass_new_file_context();
188-
context.filename->input_path = PyString_AsString(filename);
200+
context.filename->input_path = filename_v;
189201
context.filename->options.output_style = output_style_v;
190202
context.filename->options.include_paths = include_paths_v;
191203
context.filename->options.image_path = image_path_v;

sasstests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def compile_filename():
8383
body a {
8484
color: blue; }
8585
'''
86+
with raises(IOError):
87+
sass.compile(filename='test/not-exist.sass')
8688
with raises(TypeError):
8789
sass.compile(filename=1234)
8890
with raises(TypeError):

0 commit comments

Comments
 (0)