[PATCH xrandr 1/2] xrandr: Fix string constness bugs
Aaron Plattner
aplattner at nvidia.com
Fri Aug 24 21:43:12 PDT 2012
Sufficiently new versions of GCC treat string literals as "const char *" by
default. This means that several places that assign, return, or initialize
char* from a string literal generates a warning:
xrandr.c:54:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:55:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:56:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:57:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:58:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:61:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:62:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:63:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:64:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:65:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:69:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:70:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:71:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:72:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:73:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:74:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:80:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:81:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:82:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:83:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:84:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:85:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:86:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:87:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:88:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:189:2: warning: return discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:193:5: warning: return discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:202:2: warning: return discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:204:2: warning: return discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:206:2: warning: return discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:208:2: warning: return discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:210:5: warning: return discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:359:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:360:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:361:5: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:593:23: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:1189:28: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:1191:28: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:1587:39: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:1588:30: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:1589:38: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:1590:48: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:1591:42: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:1592:25: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:2544:28: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:2546:28: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:2585:28: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
xrandr.c:3228:17: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
Fix as many of these as possible. This introduces one problem:
XRRSetCrtcTransform takes a non-const char* filter parameter even though it
doesn't actually modify the string. Instead of trying to work around that, just
live with the warning for now:
xrandr.c:1459:9: warning: passing argument 4 of ‘XRRSetCrtcTransform’ discards ‘const’ qualifier from pointer target type [enabled by default]
/X/include/X11/extensions/Xrandr.h:383:1: note: expected ‘char *’ but argument is of type ‘const char *’
Signed-off-by: Aaron Plattner <aplattner at nvidia.com>
---
xrandr.c | 23 ++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/xrandr.c b/xrandr.c
index 46d133f..d05737e 100644
--- a/xrandr.c
+++ b/xrandr.c
@@ -50,14 +50,14 @@ static Bool properties = False;
static Bool grab_server = True;
static Bool no_primary = False;
-static char *direction[5] = {
+static const char *direction[5] = {
"normal",
"left",
"inverted",
"right",
"\n"};
-static char *reflections[5] = {
+static const char *reflections[5] = {
"normal",
"x",
"y",
@@ -65,7 +65,7 @@ static char *reflections[5] = {
"\n"};
/* subpixel order */
-static char *order[6] = {
+static const char *order[6] = {
"unknown",
"horizontal rgb",
"horizontal bgr",
@@ -74,7 +74,7 @@ static char *order[6] = {
"no subpixels"};
static const struct {
- char *string;
+ const char *string;
unsigned long flag;
} mode_flags[] = {
{ "+HSync", RR_HSyncPositive },
@@ -180,7 +180,7 @@ static inline double dmin (double x, double y)
return x < y ? x : y;
}
-static char *
+static const char *
rotation_name (Rotation rotation)
{
int i;
@@ -193,7 +193,7 @@ rotation_name (Rotation rotation)
return "invalid rotation";
}
-static char *
+static const char *
reflection_name (Rotation rotation)
{
rotation &= (RR_Reflect_X|RR_Reflect_Y);
@@ -270,7 +270,7 @@ typedef struct _output_prop output_prop_t;
struct _transform {
XTransform transform;
- char *filter;
+ const char *filter;
int nparams;
XFixed *params;
};
@@ -355,7 +355,7 @@ struct _umode {
name_t name;
};
-static char *connection[3] = {
+static const char *connection[3] = {
"connected",
"disconnected",
"unknown connection"};
@@ -598,11 +598,12 @@ init_transform (transform_t *transform)
static void
set_transform (transform_t *dest,
XTransform *transform,
- char *filter,
+ const char *filter,
XFixed *params,
int nparams)
{
dest->transform = *transform;
+ /* note: this string is leaked */
dest->filter = strdup (filter);
dest->nparams = nparams;
dest->params = malloc (nparams * sizeof (XFixed));
@@ -1581,7 +1582,7 @@ static void _X_NORETURN
panic (Status s, crtc_t *crtc)
{
int c = crtc->crtc.index;
- char *message;
+ const char *message;
switch (s) {
case RRSetConfigSuccess: message = "succeeded"; break;
@@ -3225,7 +3226,7 @@ main (int argc, char **argv)
printf ("\t%s: %s%s\n", XGetAtomName (dpy, props[j]),
prop, bytes_after ? "..." : "");
} else {
- char *type = actual_type ? XGetAtomName (dpy, actual_type) : "none";
+ const char *type = actual_type ? XGetAtomName (dpy, actual_type) : "none";
printf ("\t%s: %s(%d) (format %d items %d) ????\n",
XGetAtomName (dpy, props[j]),
type, (int)actual_type, actual_format, (int)nitems);
--
1.7.9.5
More information about the xorg-devel
mailing list