[PATCH libguestfs] avoid "syntax-check" failure
syntax-check failed with this: fuse/guestmount.c:364: free ((char *) r); maint.mk: don't cast free argument make: *** [sc_cast_of_argument_to_free] Error 1
From ed007e673b00ec545fcff2a708a57d98075c6460 Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Mon, 9 Nov 2009 15:06:36 +0100 Subject: [PATCH libguestfs] avoid "syntax-check" failure
* fuse/guestmount.c (fg_readlink): Don't declare "r" as const, as it is freed. --- fuse/guestmount.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fuse/guestmount.c b/fuse/guestmount.c index 910ec34..04a6a29 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -339,7 +339,7 @@ fg_access (const char *path, int mask) static int fg_readlink (const char *path, char *buf, size_t size) { - const char *r; + char *r; int free_it = 0; r = rlc_lookup (path); @@ -361,7 +361,7 @@ fg_readlink (const char *path, char *buf, size_t size) buf[len] = '\0'; if (free_it) - free ((char *) r); + free (r); return 0; } -- 1.6.5.2.351.g0943
On Mon, Nov 09, 2009 at 03:08:21PM +0100, Jim Meyering wrote:
syntax-check failed with this:
fuse/guestmount.c:364: free ((char *) r); maint.mk: don't cast free argument make: *** [sc_cast_of_argument_to_free] Error 1
From ed007e673b00ec545fcff2a708a57d98075c6460 Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Mon, 9 Nov 2009 15:06:36 +0100 Subject: [PATCH libguestfs] avoid "syntax-check" failure
* fuse/guestmount.c (fg_readlink): Don't declare "r" as const, as it is freed. --- fuse/guestmount.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/fuse/guestmount.c b/fuse/guestmount.c index 910ec34..04a6a29 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -339,7 +339,7 @@ fg_access (const char *path, int mask) static int fg_readlink (const char *path, char *buf, size_t size) { - const char *r; + char *r; int free_it = 0;
r = rlc_lookup (path); @@ -361,7 +361,7 @@ fg_readlink (const char *path, char *buf, size_t size) buf[len] = '\0';
if (free_it) - free ((char *) r); + free (r);
return 0; } -- 1.6.5.2.351.g0943
Surely this doesn't compile now, because rlc_lookup() returns a const char * which is now being assigned to a char *. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones New in Fedora 11: Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 70 libraries supprt'd http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw
Richard W.M. Jones wrote:
On Mon, Nov 09, 2009 at 03:08:21PM +0100, Jim Meyering wrote:
syntax-check failed with this:
fuse/guestmount.c:364: free ((char *) r); maint.mk: don't cast free argument make: *** [sc_cast_of_argument_to_free] Error 1
From ed007e673b00ec545fcff2a708a57d98075c6460 Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Mon, 9 Nov 2009 15:06:36 +0100 Subject: [PATCH libguestfs] avoid "syntax-check" failure
* fuse/guestmount.c (fg_readlink): Don't declare "r" as const, as it is freed. --- fuse/guestmount.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/fuse/guestmount.c b/fuse/guestmount.c index 910ec34..04a6a29 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -339,7 +339,7 @@ fg_access (const char *path, int mask) static int fg_readlink (const char *path, char *buf, size_t size) { - const char *r; + char *r; int free_it = 0;
r = rlc_lookup (path); @@ -361,7 +361,7 @@ fg_readlink (const char *path, char *buf, size_t size) buf[len] = '\0';
if (free_it) - free ((char *) r); + free (r);
return 0; } -- 1.6.5.2.351.g0943
Surely this doesn't compile now, because rlc_lookup() returns a const char * which is now being assigned to a char *.
You're right. Hadn't finished the str*cmp -> STR*EQ changes so it didn't compile yet. This isn't pretty but does the job. An alternative is to exempt the entire file from this test by adding a new .x-sc... file.
From 27e5dc065892802214860686b30abfe208fcca8b Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Mon, 9 Nov 2009 15:06:36 +0100 Subject: [PATCH libguestfs] avoid "syntax-check" failure
* fuse/guestmount.c (fg_readlink): Perform cast in a separate statement to hide it from "make syntax-check". --- fuse/guestmount.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-) diff --git a/fuse/guestmount.c b/fuse/guestmount.c index c7220c0..d45c18b 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -360,8 +360,10 @@ fg_readlink (const char *path, char *buf, size_t size) memcpy (buf, r, len); buf[len] = '\0'; - if (free_it) - free ((char *) r); + if (free_it) { + char *tmp = (char *) r; + free (tmp); + } return 0; } -- 1.6.5.2.351.g0943
On Mon, Nov 09, 2009 at 08:21:59PM +0100, Jim Meyering wrote:
From 27e5dc065892802214860686b30abfe208fcca8b Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Mon, 9 Nov 2009 15:06:36 +0100 Subject: [PATCH libguestfs] avoid "syntax-check" failure
* fuse/guestmount.c (fg_readlink): Perform cast in a separate statement to hide it from "make syntax-check". --- fuse/guestmount.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/fuse/guestmount.c b/fuse/guestmount.c index c7220c0..d45c18b 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -360,8 +360,10 @@ fg_readlink (const char *path, char *buf, size_t size) memcpy (buf, r, len); buf[len] = '\0';
- if (free_it) - free ((char *) r); + if (free_it) { + char *tmp = (char *) r; + free (tmp); + }
return 0; } -- 1.6.5.2.351.g0943
ACK. What's wrong with casting a pointer when freeing it? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://et.redhat.com/~rjones/virt-top
Richard W.M. Jones wrote:
On Mon, Nov 09, 2009 at 08:21:59PM +0100, Jim Meyering wrote:
From 27e5dc065892802214860686b30abfe208fcca8b Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Mon, 9 Nov 2009 15:06:36 +0100 Subject: [PATCH libguestfs] avoid "syntax-check" failure
* fuse/guestmount.c (fg_readlink): Perform cast in a separate statement to hide it from "make syntax-check". --- fuse/guestmount.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/fuse/guestmount.c b/fuse/guestmount.c index c7220c0..d45c18b 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -360,8 +360,10 @@ fg_readlink (const char *path, char *buf, size_t size) memcpy (buf, r, len); buf[len] = '\0';
- if (free_it) - free ((char *) r); + if (free_it) { + char *tmp = (char *) r; + free (tmp); + }
return 0; } -- 1.6.5.2.351.g0943
ACK.
What's wrong with casting a pointer when freeing it?
Nothing horribly wrong, but more of a code cleanliness issue. In the vast majority of cases (at least in old code) it's an anachronistic cast from 10-15 years ago when it was often needed. In new code, the only cases that really require the cast are like this one (which is unusual, imho). In most others, the conceptual error is in assigning to a "const" pointer something that will later be freed.
participants (2)
-
Jim Meyering -
Richard W.M. Jones