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/printf-parse\.c$
^gnulib/lib/vasnprintf\.c$
^build-aux/find-unnecessary-if-before-free$
^build-aux/useless-if-before-free$
^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
* NEWS, ChangeLog: Likewise.

View File

@ -52,3 +52,8 @@ local-checks-to-skip = \
patch-check \
check-AUTHORS \
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:
@$(srcdir)/build-aux/find-unnecessary-if-before-free \
@$(srcdir)/build-aux/useless-if-before-free \
$(useless_free_options) \
$$($(CVS_LIST_EXCEPT)) && \
{ echo '$(ME): found useless "if" before "free" above' 1>&2; \
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,8 +495,7 @@ static struct openvz_vm_def
bail_out:
free(prop);
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt);
openvzFreeVMDef(def);

View File

@ -1,7 +1,7 @@
/*
* 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
*
* This library is free software; you can redistribute it and/or
@ -1004,8 +1004,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
goto error;
}
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
/* Extract domain memory */
@ -1023,8 +1022,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
goto error;
}
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
/* Extract domain vcpu info */
obj = xmlXPathEval(BAD_CAST "string(/domain/vcpu[1])", ctxt);
@ -1039,8 +1037,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
goto error;
}
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
/* See if ACPI feature is requested */
obj = xmlXPathEval(BAD_CAST "/domain/features/acpi", ctxt);
@ -1062,8 +1059,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
else
def->noReboot = 0;
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
/* See if we set clock to localtime */
obj = xmlXPathEval(BAD_CAST "string(/domain/clock/@offset)", ctxt);
@ -1076,8 +1072,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
else
def->localtime = 0;
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
/* Extract OS type info */
@ -1111,8 +1106,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
}
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);
if ((obj == NULL) || (obj->type != XPATH_STRING) ||
@ -1134,8 +1128,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
}
strcpy(def->os.machine, (const char *)obj->stringval);
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "string(/domain/os/kernel[1])", ctxt);
@ -1147,8 +1140,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
}
strcpy(def->os.kernel, (const char *)obj->stringval);
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "string(/domain/os/initrd[1])", ctxt);
@ -1160,8 +1152,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
}
strcpy(def->os.initrd, (const char *)obj->stringval);
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "string(/domain/os/cmdline[1])", ctxt);
@ -1173,8 +1164,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
}
strcpy(def->os.cmdline, (const char *)obj->stringval);
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
/* analysis of the disk devices */
@ -1224,8 +1214,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
}
strcpy(def->os.binary, (const char *)obj->stringval);
}
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics", ctxt);
if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
@ -1382,8 +1371,7 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
error:
free(prop);
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt);
qemudFreeVMDef(def);
return NULL;
@ -2389,10 +2377,8 @@ static struct qemud_network_def *qemudParseNetworkXML(virConnectPtr conn,
error:
/* XXX free all the stuff in the qemud_network struct, or leave it upto
the caller ? */
if (obj)
xmlXPathFreeObject(obj);
if (tmp)
xmlXPathFreeObject(tmp);
xmlXPathFreeObject(obj);
xmlXPathFreeObject(tmp);
xmlXPathFreeContext(ctxt);
qemudFreeNetworkDef(def);
return NULL;

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
/*
* 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
*
@ -119,8 +119,7 @@ checkRequestValue(const char *xmlstr, const char *xpath, int type, void *expecte
ret = 0;
error:
if (obj)
xmlXPathFreeObject(obj);
xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt);
if (xml)
xmlFreeDoc(xml);