Remove more useless if tests before "free"-like functions.

* build-aux/useless-if-before-free: Rename from ...
* build-aux/find-unnecessary-if-before-free: ... this.  Remove file.
Also changed it so that new names are no longer hard-coded in the
script.  Instead, they're supplied via options:
* Makefile.cfg (useless_free_options): Define.
Add xmlXPathFreeObject to the list of free-like functions it detects.
* Makefile.maint (sc_avoid_if_before_free): Reflect script renaming.
* .x-sc_avoid_if_before_free: Likewise.
* src/openvz_conf.c (openvzParseXML): Remove useless "if"-before-free.
* src/qemu_conf.c (qemudParseXML, qemudParseNetworkXML): Likewise.
* src/virsh.c (cmdVNCDisplay, cmdTTYConsole, cmdDetachInterface):
(cmdDetachDisk): Likewise.
* src/xm_internal.c (xenXMConfigSetIntFromXPath): Likewise.
(xenXMConfigSetStringFromXPath, xenXMParseXMLToConfig): Likewise.
(xenXMDomainAttachDevice, xenXMAttachDisk, xenXMAttachInterface):
(xenXMDomainDetachDevice): Likewise.
* src/xml.c (virXPathString): Likewise.
* tests/xmlrpctest.c (checkRequestValue): Likewise.
This commit is contained in:
Jim Meyering 2008-02-07 16:49:29 +00:00
parent 5a93214143
commit e8ff93b4e6
12 changed files with 190 additions and 129 deletions

View File

@ -1,5 +1,5 @@
^gnulib/lib/getaddrinfo\.c$ ^gnulib/lib/getaddrinfo\.c$
^gnulib/lib/printf-parse\.c$ ^gnulib/lib/printf-parse\.c$
^gnulib/lib/vasnprintf\.c$ ^gnulib/lib/vasnprintf\.c$
^build-aux/find-unnecessary-if-before-free$ ^build-aux/useless-if-before-free$
^ChangeLog$ ^ChangeLog$

View File

@ -1,4 +1,24 @@
2008-02-07 Jim Meyering <meyering@redhat.com> Thu Feb 7 17:48:30 CET 2008 Jim Meyering <meyering@redhat.com>
Remove more useless if tests before "free"-like functions.
* build-aux/useless-if-before-free: Rename from ...
* build-aux/find-unnecessary-if-before-free: ... this. Remove file.
Also changed it so that new names are no longer hard-coded in the
script. Instead, they're supplied via options:
* Makefile.cfg (useless_free_options): Define.
Add xmlXPathFreeObject to the list of free-like functions it detects.
* Makefile.maint (sc_avoid_if_before_free): Reflect script renaming.
* .x-sc_avoid_if_before_free: Likewise.
* src/openvz_conf.c (openvzParseXML): Remove useless "if"-before-free.
* src/qemu_conf.c (qemudParseXML, qemudParseNetworkXML): Likewise.
* src/virsh.c (cmdVNCDisplay, cmdTTYConsole, cmdDetachInterface):
(cmdDetachDisk): Likewise.
* src/xm_internal.c (xenXMConfigSetIntFromXPath): Likewise.
(xenXMConfigSetStringFromXPath, xenXMParseXMLToConfig): Likewise.
(xenXMDomainAttachDevice, xenXMAttachDisk, xenXMAttachInterface):
(xenXMDomainDetachDevice): Likewise.
* src/xml.c (virXPathString): Likewise.
* tests/xmlrpctest.c (checkRequestValue): Likewise.
* src/xm_internal.c: Remove trailing blanks * src/xm_internal.c: Remove trailing blanks
* NEWS, ChangeLog: Likewise. * NEWS, ChangeLog: Likewise.

View File

@ -52,3 +52,8 @@ local-checks-to-skip = \
patch-check \ patch-check \
check-AUTHORS \ check-AUTHORS \
changelog-check changelog-check
useless_free_options = \
--name=sexpr_free \
--name=xmlXPathFreeContext \
--name=xmlXPathFreeObject

View File

@ -40,7 +40,8 @@ syntax-check: $(local-check)
## --------------- ## ## --------------- ##
sc_avoid_if_before_free: sc_avoid_if_before_free:
@$(srcdir)/build-aux/find-unnecessary-if-before-free \ @$(srcdir)/build-aux/useless-if-before-free \
$(useless_free_options) \
$$($(CVS_LIST_EXCEPT)) && \ $$($(CVS_LIST_EXCEPT)) && \
{ echo '$(ME): found useless "if" before "free" above' 1>&2; \ { echo '$(ME): found useless "if" before "free" above' 1>&2; \
exit 1; } || : exit 1; } || :

View File

@ -1,42 +0,0 @@
#!/usr/bin/perl
# Detect instances of "if (p) free (p);".
# Likewise for "if (p != NULL) free (p);".
# Exit status is like grep: 0 for no match. 1 for any number.
# Note: giving line numbers isn't practical, since I've reset the
# input record separator.
use strict;
use warnings;
(my $ME = $0) =~ s|.*/||;
{
# Use ';' as the input record separator.
$/ = ';';
my $found_match = 0;
foreach my $file (@ARGV)
{
open FH, '<', $file
or die "$ME: can't open `$file' for reading: $!\n";
while (defined (my $line = <FH>))
{
if ($line =~
/\b(if\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)
\s+(?:xmlXPathFreeContext|(?:sexpr_)?free)\s*\(\s*\2\s*\))/sx)
{
print "$file: $1\n";
$found_match = 1;
}
}
close FH;
}
exit !$found_match;
}
my $foo = <<'EOF';
# The above is to *find* them.
# This adjusts them, removing the unnecessary "if (p)" part.
git ls-files -z --exclude=find-unnecessary-if-before-free |xargs -0 \
perl -0x3b -pi -e 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+((?:xmlXPathFreeContext|(?:sexpr_)?free)\s*\(\s*\1\s*\))/$2/s'
EOF

118
build-aux/useless-if-before-free Executable file
View File

@ -0,0 +1,118 @@
#!/usr/bin/perl -T
# Detect instances of "if (p) free (p);".
# Likewise for "if (p != NULL) free (p);".
my $VERSION = '2008-02-04 22:25'; # UTC
# The definition above must lie within the first 8 lines in order
# for the Emacs time-stamp write hook (at end) to update it.
# If you change this file with Emacs, please let the write hook
# do its job. Otherwise, update this string manually.
# Exit status is like grep: 0 for no match. 1 for any number.
# Note: giving line numbers isn't practical, since I've reset the
# input record separator.
use strict;
use warnings;
use Getopt::Long;
(my $ME = $0) =~ s|.*/||;
my $debug = 0;
my $verbose = 0;
sub usage ($)
{
my ($exit_code) = @_;
my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
if ($exit_code != 0)
{
print $STREAM "Try `$ME --help' for more information.\n";
}
else
{
print $STREAM <<EOF;
Usage: $ME [OPTIONS] FILE...
OPTIONS:
--name=N add name N to the list of `free'-like functions to detect;
may be repeated
--help display this help and exit
--version output version information and exit
--verbose generate verbose output
EXAMPLE:
git ls-files -z |xargs -0 $ME
EOF
}
exit $exit_code;
}
{
my @name = qw(free);
GetOptions
(
debug => \$debug,
verbose => \$verbose,
help => sub { usage 0 },
version => sub { print "$ME version $VERSION\n"; exit },
'name=s@' => \@name,
) or usage 1;
# Make sure we have the right number of non-option arguments.
# Always tell the user why we fail.
@ARGV < 1
and (warn "$ME: missing FILE argument\n"), usage 1;
my $or = join '|', @name;
my $regexp = qr/(?:$or)/;
# Set the input record separator.
$/ = '"';
my $found_match = 0;
foreach my $file (@ARGV)
{
open FH, '<', $file
or die "$ME: can't open `$file' for reading: $!\n";
while (defined (my $line = <FH>))
{
if ($line =~
/\b(if\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)
(?: \s*$regexp\s*\(\s*\2\s*\)|
\s*\{\s*$regexp\s*\(\s*\2\s*\)\s*;\s*\}))/sx)
{
print "$file: $1\n";
$found_match = 1;
}
}
close FH;
}
exit !$found_match;
}
my $foo = <<'EOF';
# The above is to *find* them.
# This adjusts them, removing the unnecessary "if (p)" part.
# FIXME: do something like this as an option.
git ls-files -z --exclude=$ME |xargs -0 \
perl -0x3b -pi -e 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+((?:sexpr_)?free\s*\(\s*\1\s*\))/$2/s'
When modifying files, refuse to process anything other than a regular file.
# Or this one-liner to detect them:
git ls-files -z |xargs -0 perl -00 -ne '/\b(if\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)(?:\s*(?:sexpr_)?free\s*\(\s*\2\s*\)|\s*\{\s*(?:sexpr_)?free\s*\(\s*\2\s*\)\s*;\s*\}))/sx and print "$1\n"'
EOF
## Local Variables:
## indent-tabs-mode: nil
## eval: (add-hook 'write-file-hooks 'time-stamp)
## time-stamp-start: "my $VERSION = '"
## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
## time-stamp-time-zone: "UTC"
## time-stamp-end: "'; # UTC"
## End:

View File

@ -495,7 +495,6 @@ static struct openvz_vm_def
bail_out: bail_out:
free(prop); free(prop);
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
openvzFreeVMDef(def); openvzFreeVMDef(def);

View File

@ -1,7 +1,7 @@
/* /*
* config.c: VM configuration management * config.c: VM configuration management
* *
* Copyright (C) 2006, 2007 Red Hat, Inc. * Copyright (C) 2006, 2007, 2008 Red Hat, Inc.
* Copyright (C) 2006 Daniel P. Berrange * Copyright (C) 2006 Daniel P. Berrange
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -1004,7 +1004,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
goto error; goto error;
} }
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
@ -1023,7 +1022,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
goto error; goto error;
} }
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
/* Extract domain vcpu info */ /* Extract domain vcpu info */
@ -1039,7 +1037,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
goto error; goto error;
} }
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
/* See if ACPI feature is requested */ /* See if ACPI feature is requested */
@ -1062,7 +1059,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
else else
def->noReboot = 0; def->noReboot = 0;
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
/* See if we set clock to localtime */ /* See if we set clock to localtime */
@ -1076,7 +1072,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
else else
def->localtime = 0; def->localtime = 0;
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
@ -1111,7 +1106,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
} }
strcpy(def->os.arch, (const char *)obj->stringval); strcpy(def->os.arch, (const char *)obj->stringval);
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "string(/domain/os/type[1]/@machine)", ctxt); obj = xmlXPathEval(BAD_CAST "string(/domain/os/type[1]/@machine)", ctxt);
@ -1134,7 +1128,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
} }
strcpy(def->os.machine, (const char *)obj->stringval); strcpy(def->os.machine, (const char *)obj->stringval);
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
@ -1147,7 +1140,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
} }
strcpy(def->os.kernel, (const char *)obj->stringval); strcpy(def->os.kernel, (const char *)obj->stringval);
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
@ -1160,7 +1152,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
} }
strcpy(def->os.initrd, (const char *)obj->stringval); strcpy(def->os.initrd, (const char *)obj->stringval);
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
@ -1173,7 +1164,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
} }
strcpy(def->os.cmdline, (const char *)obj->stringval); strcpy(def->os.cmdline, (const char *)obj->stringval);
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
@ -1224,7 +1214,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
} }
strcpy(def->os.binary, (const char *)obj->stringval); strcpy(def->os.binary, (const char *)obj->stringval);
} }
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics", ctxt); obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics", ctxt);
@ -1382,7 +1371,6 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
error: error:
free(prop); free(prop);
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
qemudFreeVMDef(def); qemudFreeVMDef(def);
@ -2389,9 +2377,7 @@ static struct qemud_network_def *qemudParseNetworkXML(virConnectPtr conn,
error: error:
/* XXX free all the stuff in the qemud_network struct, or leave it upto /* XXX free all the stuff in the qemud_network struct, or leave it upto
the caller ? */ the caller ? */
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
if (tmp)
xmlXPathFreeObject(tmp); xmlXPathFreeObject(tmp);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
qemudFreeNetworkDef(def); qemudFreeNetworkDef(def);

View File

@ -2931,7 +2931,6 @@ cmdVNCDisplay(vshControl * ctl, vshCmd * cmd)
ret = TRUE; ret = TRUE;
cleanup: cleanup:
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
if (xml) if (xml)
@ -2993,7 +2992,6 @@ cmdTTYConsole(vshControl * ctl, vshCmd * cmd)
vshPrint(ctl, "%s\n", (const char *)obj->stringval); vshPrint(ctl, "%s\n", (const char *)obj->stringval);
cleanup: cleanup:
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
if (xml) if (xml)
@ -3338,7 +3336,6 @@ cmdDetachInterface(vshControl * ctl, vshCmd * cmd)
cleanup: cleanup:
if (dom) if (dom)
virDomainFree(dom); virDomainFree(dom);
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
if (xml) if (xml)
@ -3611,7 +3608,6 @@ cmdDetachDisk(vshControl * ctl, vshCmd * cmd)
ret = TRUE; ret = TRUE;
cleanup: cleanup:
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
if (xml) if (xml)

View File

@ -1558,7 +1558,6 @@ int xenXMConfigSetIntFromXPath(virConnectPtr conn,
ret = 0; ret = 0;
error: error:
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
return ret; return ret;
@ -1593,7 +1592,6 @@ int xenXMConfigSetStringFromXPath(virConnectPtr conn,
ret = 0; ret = 0;
error: error:
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
return ret; return ret;
@ -2280,7 +2278,6 @@ virConfPtr xenXMParseXMLToConfig(virConnectPtr conn, const char *xml) {
virConfFree(conf); virConfFree(conf);
if (prop != NULL) if (prop != NULL)
xmlFree(prop); xmlFree(prop);
if (obj != NULL)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
if (doc != NULL) if (doc != NULL)
@ -2580,7 +2577,6 @@ xenXMDomainAttachDevice(virDomainPtr domain, const char *xml) {
(obj->stringval) && (STREQ((char *)obj->stringval, "hvm"))) (obj->stringval) && (STREQ((char *)obj->stringval, "hvm")))
hvm = 1; hvm = 1;
if (ctxt)
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
ctxt = NULL; ctxt = NULL;
if (doc) if (doc)
@ -2619,11 +2615,8 @@ xenXMDomainAttachDevice(virDomainPtr domain, const char *xml) {
ret = 0; ret = 0;
cleanup: cleanup:
if (domxml)
free(domxml); free(domxml);
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
if (ctxt)
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
if (doc) if (doc)
xmlFreeDoc(doc); xmlFreeDoc(doc);
@ -2738,7 +2731,6 @@ xenXMAttachDisk(virDomainPtr domain, xmlXPathContextPtr ctxt, int hvm,
prev->next = list_val; prev->next = list_val;
} else { } else {
/* configure */ /* configure */
if (list_val->str)
free(list_val->str); free(list_val->str);
list_val->str = dev; list_val->str = dev;
} }
@ -2747,11 +2739,8 @@ xenXMAttachDisk(virDomainPtr domain, xmlXPathContextPtr ctxt, int hvm,
goto cleanup; goto cleanup;
cleanup: cleanup:
if (type)
free(type); free(type);
if (source)
free(source); free(source);
if (target)
free(target); free(target);
return (ret); return (ret);
@ -2825,7 +2814,6 @@ xenXMAttachInterface(virDomainPtr domain, xmlXPathContextPtr ctxt, int hvm,
if (!(strcmp(dommac, (const char *) mac))) { if (!(strcmp(dommac, (const char *) mac))) {
if (autoassign) { if (autoassign) {
if (mac)
free(mac); free(mac);
mac = NULL; mac = NULL;
if (!(mac = (xmlChar *)xenXMAutoAssignMac())) if (!(mac = (xmlChar *)xenXMAutoAssignMac()))
@ -2914,7 +2902,6 @@ xenXMAttachInterface(virDomainPtr domain, xmlXPathContextPtr ctxt, int hvm,
prev->next = list_val; prev->next = list_val;
} else { } else {
/* configure */ /* configure */
if (list_val->str)
free(list_val->str); free(list_val->str);
list_val->str = dev; list_val->str = dev;
} }
@ -2930,11 +2917,8 @@ xenXMAttachInterface(virDomainPtr domain, xmlXPathContextPtr ctxt, int hvm,
if (text_node) if (text_node)
xmlFree(text_node); xmlFree(text_node);
cleanup: cleanup:
if (type)
free(type); free(type);
if (source)
free(source); free(source);
if (mac)
free(mac); free(mac);
return (ret); return (ret);
@ -3139,15 +3123,11 @@ xenXMDomainDetachDevice(virDomainPtr domain, const char *xml) {
ret = 0; ret = 0;
cleanup: cleanup:
if (ctxt)
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
if (doc) if (doc)
xmlFreeDoc(doc); xmlFreeDoc(doc);
if (domdevice)
free(domdevice); free(domdevice);
if (key)
free(key); free(key);
if (list_val)
free(list_val); free(list_val);
return (ret); return (ret);

View File

@ -479,7 +479,6 @@ virXPathString(const char *xpath, xmlXPathContextPtr ctxt)
obj = xmlXPathEval(BAD_CAST xpath, ctxt); obj = xmlXPathEval(BAD_CAST xpath, ctxt);
if ((obj == NULL) || (obj->type != XPATH_STRING) || if ((obj == NULL) || (obj->type != XPATH_STRING) ||
(obj->stringval == NULL) || (obj->stringval[0] == 0)) { (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
return (NULL); return (NULL);
} }

View File

@ -1,7 +1,7 @@
/* /*
* xmlrpctest.c: simple client for XML-RPC tests * xmlrpctest.c: simple client for XML-RPC tests
* *
* Copyright (C) 2005 Red Hat, Inc. * Copyright (C) 2005, 2008 Red Hat, Inc.
* *
* See COPYING.LIB for the License of this software * See COPYING.LIB for the License of this software
* *
@ -119,7 +119,6 @@ checkRequestValue(const char *xmlstr, const char *xpath, int type, void *expecte
ret = 0; ret = 0;
error: error:
if (obj)
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
if (xml) if (xml)