[Rpm-maint] [PATCH] mark strings extracted from PyArg_Parse* as "const"

David Malcolm dmalcolm at redhat.com
Wed Dec 21 22:40:44 UTC 2011


Various places within the bindings use PyArg_ParseTuple[AndKeywords]
to extract (char*) string arguments.

These are pointers to the internal representation of a PyStringObject,
and shouldn't be modified, hence it's safest to explicitly mark these
values as (const char*), rather than just (char*).

Doing so suppresses various warnings about the above reported by the
experimental static analysis tool I've been writing:
   http://gcc-python-plugin.readthedocs.org/en/latest/cpychecker.html
which is part of https://fedorahosted.org/gcc-python-plugin
---
 python/header-py.c   |    4 ++--
 python/rpmfd-py.c    |    4 ++--
 python/rpmmacro-py.c |    6 +++---
 python/rpmmi-py.c    |    2 +-
 python/rpmmodule.c   |    4 ++--
 python/rpmts-py.c    |    2 +-
 6 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/python/header-py.c b/python/header-py.c
index 9e245aa..239ef7f 100644
--- a/python/header-py.c
+++ b/python/header-py.c
@@ -232,7 +232,7 @@ static PyObject * hdrFullFilelist(hdrObject * s)
 
 static PyObject * hdrFormat(hdrObject * s, PyObject * args, PyObject * kwds)
 {
-    char * fmt;
+    const char * fmt;
     char * r;
     errmsg_t err;
     PyObject * result;
@@ -859,7 +859,7 @@ static int compare_values(const char *str1, const char *str2)
 
 PyObject * labelCompare (PyObject * self, PyObject * args)
 {
-    char *v1, *r1, *v2, *r2;
+    const char *v1, *r1, *v2, *r2;
     const char *e1, *e2;
     int rc;
 
diff --git a/python/rpmfd-py.c b/python/rpmfd-py.c
index 1150aa1..2d443f3 100644
--- a/python/rpmfd-py.c
+++ b/python/rpmfd-py.c
@@ -57,8 +57,8 @@ static FD_t openPath(const char *path, const char *mode, const char *flags)
 static int rpmfd_init(rpmfdObject *s, PyObject *args, PyObject *kwds)
 {
     char *kwlist[] = { "obj", "mode", "flags", NULL };
-    char *mode = "r";
-    char *flags = "ufdio";
+    const char *mode = "r";
+    const char *flags = "ufdio";
     PyObject *fo = NULL;
     FD_t fd = NULL;
     int fdno;
diff --git a/python/rpmmacro-py.c b/python/rpmmacro-py.c
index 074861d..3eb0acb 100644
--- a/python/rpmmacro-py.c
+++ b/python/rpmmacro-py.c
@@ -7,7 +7,7 @@
 PyObject *
 rpmmacro_AddMacro(PyObject * self, PyObject * args, PyObject * kwds)
 {
-    char * name, * val;
+    const char * name, * val;
     char * kwlist[] = {"name", "value", NULL};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss:AddMacro", kwlist,
@@ -22,7 +22,7 @@ rpmmacro_AddMacro(PyObject * self, PyObject * args, PyObject * kwds)
 PyObject *
 rpmmacro_DelMacro(PyObject * self, PyObject * args, PyObject * kwds)
 {
-    char * name;
+    const char * name;
     char * kwlist[] = {"name", NULL};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:DelMacro", kwlist, &name))
@@ -36,7 +36,7 @@ rpmmacro_DelMacro(PyObject * self, PyObject * args, PyObject * kwds)
 PyObject * 
 rpmmacro_ExpandMacro(PyObject * self, PyObject * args, PyObject * kwds)
 {
-    char *macro;
+    const char *macro;
     PyObject *res;
     int num = 0;
     char * kwlist[] = {"macro", "numeric", NULL};
diff --git a/python/rpmmi-py.c b/python/rpmmi-py.c
index 91eb21c..977e475 100644
--- a/python/rpmmi-py.c
+++ b/python/rpmmi-py.c
@@ -99,7 +99,7 @@ static PyObject *
 rpmmi_Pattern(rpmmiObject * s, PyObject * args, PyObject * kwds)
 {
     int type;
-    char * pattern;
+    const char * pattern;
     rpmTagVal tag;
     char * kwlist[] = {"tag", "type", "patern", NULL};
 
diff --git a/python/rpmmodule.c b/python/rpmmodule.c
index f9149cb..f2e0ed7 100644
--- a/python/rpmmodule.c
+++ b/python/rpmmodule.c
@@ -28,7 +28,7 @@ PyObject * pyrpmError;
 
 static PyObject * archScore(PyObject * self, PyObject * arg)
 {
-    char * arch;
+    const char * arch;
 
     if (!PyArg_Parse(arg, "s", &arch))
 	return NULL;
@@ -117,7 +117,7 @@ static PyObject * doLog(PyObject * self, PyObject * args, PyObject *kwds)
 
 static PyObject * reloadConfig(PyObject * self, PyObject * args, PyObject *kwds)
 {
-    char * target = NULL;
+    const char * target = NULL;
     char * kwlist[] = { "target", NULL };
     int rc;
     
diff --git a/python/rpmts-py.c b/python/rpmts-py.c
index 854d3a1..5c6e9ba 100644
--- a/python/rpmts-py.c
+++ b/python/rpmts-py.c
@@ -751,7 +751,7 @@ static PyObject * rpmts_new(PyTypeObject * subtype, PyObject *args, PyObject *kw
 
 static int rpmts_init(rpmtsObject *s, PyObject *args, PyObject *kwds)
 {
-    char * rootDir = "/";
+    const char * rootDir = "/";
     rpmVSFlags vsflags = rpmExpandNumeric("%{?__vsflags}");
     char * kwlist[] = {"rootdir", "vsflags", 0};
 
-- 
1.7.6.2




More information about the Rpm-maint mailing list