cluster-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
From: Jan Pokorný <jpokorny@redhat.com>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCH] rgmanager: resrules: simplify getting attributes + avoid memleaks
Date: Mon, 2 Apr 2012 18:29:46 +0200	[thread overview]
Message-ID: <20120402162946.GA28796@redhat.com> (raw)
In-Reply-To: <20120402153832.GA27549@redhat.com>

These memleaks (connected with attrname, dflt and ret) could occur
in some recoverable error corner cases.

Getting of default value is postponed until it is actually needed
(i.e., the value is not inherited which should have the priority,
at least as per in-code comment).

update v2:
Now, also avoid memleak upon store_attribute failure.

Signed-off-by: Jan Pokorn? <jpokorny@redhat.com>
---
 resrules.c |   39 +++++++++++++++++----------------------
 1 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/resrules.c b/resrules.c
index 353ead4..557ef9c 100644
--- a/resrules.c
+++ b/resrules.c
@@ -711,28 +711,20 @@ static int
 _get_rule_attrs(xmlDocPtr doc, xmlXPathContextPtr ctx, char *base,
 		resource_rule_t *rr)
 {
-	char *ret, *attrname, *dflt = NULL, xpath[256];
+	char *ret, *attrname, xpath[256];
 	int x, flags, primary_found = 0;
 
 	for (x = 1; 1; x++) {
 		snprintf(xpath, sizeof(xpath), "%s/parameter[%d]/@name",
  			 base, x);
 
-		ret = xpath_get_one(doc,ctx,xpath);
-		if (!ret)
+		attrname = xpath_get_one(doc,ctx,xpath);
+		if (!attrname)
 			break;
 
 		flags = 0;
-		attrname = ret;
 		
 		/*
-		   See if there's a default value.
-		 */
-		snprintf(xpath, sizeof(xpath),
-			 "%s/parameter[%d]/content/@default", base, x);
-		dflt = xpath_get_one(doc,ctx,xpath);
-
-		/*
 		   See if this is either the primary identifier or
 		   a required field.
 		 */
@@ -761,6 +753,7 @@ _get_rule_attrs(xmlDocPtr doc, xmlXPathContextPtr ctx, char *base,
 			if ((atoi(ret) != 0) || (ret[0] == 'y')) {
 				if (primary_found) {
 					free(ret);
+					free(attrname);
 					printf("Multiple primary "
 					       "definitions for "
 					       "resource type %s\n",
@@ -786,7 +779,8 @@ _get_rule_attrs(xmlDocPtr doc, xmlXPathContextPtr ctx, char *base,
 		}
 
 		/*
-		   See if this is supposed to be inherited
+		   See if this is supposed to be inherited;
+		   inheritance supercedes a specified default value
 		 */
 		snprintf(xpath, sizeof(xpath), "%s/parameter[%d]/@inherit",
 			 base, x);
@@ -795,31 +789,32 @@ _get_rule_attrs(xmlDocPtr doc, xmlXPathContextPtr ctx, char *base,
 
 			if (flags & (RA_REQUIRED | RA_PRIMARY | RA_UNIQUE)) {
 				free(ret);
+				free(attrname);
 				printf("Can not inherit and be primary, "
 				       "unique, or required\n");
 				return -1;
 			}
-			/*
-			   don't free ret.  Store as attr value.  If we had
-			   a default value specified from above, free it;
-			   inheritance supercedes a specified default value.
-			 */
-			if (dflt)
-				free(dflt);
+
+			/* Don't free ret */
+
 		} else {
 			/*
 			   Use default value, if specified, as the attribute
 			   value.
 			 */
-			ret = dflt;
+			snprintf(xpath, sizeof(xpath),
+				 "%s/parameter[%d]/content/@default", base, x);
+			ret = xpath_get_one(doc,ctx,xpath);
 		}
 
 		/*
 		   Store the attribute.  We'll ensure all required
 		   attributes are present soon.
 		 */
-		if (attrname)
-			store_attribute(&rr->rr_attrs, attrname, ret, flags);
+		if (store_attribute(&rr->rr_attrs, attrname, ret, flags) != 0) {
+			free(attrname);
+			free(ret);
+		}
 	}
 
 	return 0;



      reply	other threads:[~2012-04-02 16:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-02 15:38 [Cluster-devel] [PATCH] rgmanager: resrules: simplify reading attributes + avoid memleaks Jan Pokorný
2012-04-02 16:29 ` Jan Pokorný [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120402162946.GA28796@redhat.com \
    --to=jpokorny@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).