From 11ec71fe19b72a35612fa52cef5c25760e487d7d Mon Sep 17 00:00:00 2001 From: nima Date: Fri, 19 Dec 2008 13:21:24 +0000 Subject: 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 --- src/dmidecodemodule.c | 31 ++++++++++++++++++++++--------- src/dmidecodemodule.h | 3 +++ 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/dmidecodemodule.c b/src/dmidecodemodule.c index 8aec347..a37344d 100644 --- a/src/dmidecodemodule.c +++ b/src/dmidecodemodule.c @@ -158,25 +158,38 @@ static PyObject* dmidecode_get_type(PyObject *self, PyObject *args) { } static PyObject* dmidecode_dump(PyObject *self, PyObject *args) { - if(access(opt.dumpfile, W_OK) == 0) + const char *f = PyString_AsString(opt.dumpfile); + if((access(f, F_OK) != 0) || (access(f, W_OK) == 0)) if(dump(PyString_AS_STRING(opt.dumpfile))) Py_RETURN_TRUE; Py_RETURN_FALSE; } static PyObject* dmidecode_get_dev(PyObject *self, PyObject *null) { - if(opt.dumpfile != NULL) return opt.dumpfile; - else return PyString_FromString(opt.devmem); + PyObject *dev; + if(opt.dumpfile != NULL) dev = opt.dumpfile; + else dev = PyString_FromString(opt.devmem); + Py_INCREF(dev); + return dev; } static PyObject* dmidecode_set_dev(PyObject *self, PyObject *arg) { if(PyString_Check(arg)) { - if(opt.dumpfile) { Py_DECREF(opt.dumpfile); } - opt.dumpfile = arg; - Py_INCREF(opt.dumpfile); - Py_RETURN_TRUE; - } else Py_RETURN_FALSE; - //PyErr_Occurred() + if(opt.dumpfile == arg) Py_RETURN_TRUE; + + struct stat buf; + char *f = PyString_AsString(arg); + stat(f, &buf); + if(!S_ISDIR(buf.st_mode)) { + if(opt.dumpfile) + Py_DECREF(opt.dumpfile); + opt.dumpfile = arg; + Py_INCREF(opt.dumpfile); + Py_RETURN_TRUE; + } + } + Py_RETURN_FALSE; + //PyErr_Occurred(); } diff --git a/src/dmidecodemodule.h b/src/dmidecodemodule.h index 02131c6..89eea87 100644 --- a/src/dmidecodemodule.h +++ b/src/dmidecodemodule.h @@ -3,6 +3,9 @@ #include #include #include + +#include +#include #include #include "version.h" -- cgit