Skip to content

Commit 11ec71f

Browse files
author
nima
committed
Check that the path given with set_dev() is writeable.
Don't crash when writing to a read-only file, return False instead. Missing an INCREF in get_dev() fixed. git-svn-id: svn://svn.autonomy.net.au/python-dmidecode@124 abc39116-655e-4be6-ad55-d661dc543056
1 parent b007b77 commit 11ec71f

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

src/dmidecodemodule.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,25 +158,38 @@ static PyObject* dmidecode_get_type(PyObject *self, PyObject *args) {
158158
}
159159

160160
static PyObject* dmidecode_dump(PyObject *self, PyObject *args) {
161-
if(access(opt.dumpfile, W_OK) == 0)
161+
const char *f = PyString_AsString(opt.dumpfile);
162+
if((access(f, F_OK) != 0) || (access(f, W_OK) == 0))
162163
if(dump(PyString_AS_STRING(opt.dumpfile)))
163164
Py_RETURN_TRUE;
164165
Py_RETURN_FALSE;
165166
}
166167

167168
static PyObject* dmidecode_get_dev(PyObject *self, PyObject *null) {
168-
if(opt.dumpfile != NULL) return opt.dumpfile;
169-
else return PyString_FromString(opt.devmem);
169+
PyObject *dev;
170+
if(opt.dumpfile != NULL) dev = opt.dumpfile;
171+
else dev = PyString_FromString(opt.devmem);
172+
Py_INCREF(dev);
173+
return dev;
170174
}
171175

172176
static PyObject* dmidecode_set_dev(PyObject *self, PyObject *arg) {
173177
if(PyString_Check(arg)) {
174-
if(opt.dumpfile) { Py_DECREF(opt.dumpfile); }
175-
opt.dumpfile = arg;
176-
Py_INCREF(opt.dumpfile);
177-
Py_RETURN_TRUE;
178-
} else Py_RETURN_FALSE;
179-
//PyErr_Occurred()
178+
if(opt.dumpfile == arg) Py_RETURN_TRUE;
179+
180+
struct stat buf;
181+
char *f = PyString_AsString(arg);
182+
stat(f, &buf);
183+
if(!S_ISDIR(buf.st_mode)) {
184+
if(opt.dumpfile)
185+
Py_DECREF(opt.dumpfile);
186+
opt.dumpfile = arg;
187+
Py_INCREF(opt.dumpfile);
188+
Py_RETURN_TRUE;
189+
}
190+
}
191+
Py_RETURN_FALSE;
192+
//PyErr_Occurred();
180193
}
181194

182195

src/dmidecodemodule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include <stdio.h>
44
#include <string.h>
55
#include <stdlib.h>
6+
7+
#include <sys/types.h>
8+
#include <sys/stat.h>
69
#include <unistd.h>
710

811
#include "version.h"

0 commit comments

Comments
 (0)