Skip to contentSkip to conversation

Four more pre-requisites for mountd IPv6 support

PATCH In review Chuck Lever 3 participants 7 messages Apr 19, 2010 – Apr 23, 2010 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Four more pre-requisites for mountd IPv6 support

Hi Steve-

These patches clean up some memory allocation nits in libexport.a. They pave the way for parsing IPv6 addresses and netmasks in client_init().

---

Chuck Lever (4):
libexport.a: Allow malloc(3) failures in client_lookup() and friends libexport.a: Allow m_hostname allocation to fail instead of exit libexport.a: Allow client_init() to fail instead of exit libexport.a: Add client_free()

 support/export/client.c |   55 +++++++++++++++++++++++++++++++----------------
 1 files changed, 36 insertions(+), 19 deletions(-)
Signature · 1 line
Chuck Lever

A message that is not in the archive (sent off-list or not yet received): 20100419201855.3567.9644.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org

#2 lore raw

PATCH 1/4 libexport.a: Add client_free()

Clean up: Introduce a helper to free a nfs_client record.

---

 support/export/client.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
support/export/client.c +8 -2
index 6a25928..859dcf1 100644
@@ -52,6 +52,13 @@ init_addrlist(nfs_client *clp, const struct hostent *hp)
 	clp->m_naddr = i;
 }
 
+static void
+client_free(nfs_client *clp)
+{
+	xfree(clp->m_hostname);
+	xfree(clp);
+}
+
 /* if canonical is set, then we *know* this is already a canonical name
  * so hostname lookup is avoided.
  * This is used when reading /proc/fs/nfs/exports
@@ -209,8 +216,7 @@ client_freeall(void)
 		head = clientlist + i;
 		while (*head) {
 			*head = (clp = *head)->m_next;
-			xfree(clp->m_hostname);
-			xfree(clp);
+			client_free(clp);
 		}
 	}
 }
#3 lore raw

PATCH 2/4 libexport.a: Allow client_init() to fail instead of exit

In reply to a message not in the archive

client_init()'s current callers can now deal correctly with a failure. Get rid of code that can cause our process to exit in client_init(), if address mask parsing or memory allocation fails.

---

 support/export/client.c |   24 +++++++++++++++++-------
 1 files changed, 17 insertions(+), 7 deletions(-)
support/export/client.c +17 -7
index 859dcf1..8f83da3 100644
@@ -30,7 +30,7 @@ extern int	innetgr(char *netgr, char *host, char *, char *);
 #endif
 
 static char	*add_name(char *old, const char *add);
-static void	client_init(nfs_client *clp, const char *hname,
+static int	client_init(nfs_client *clp, const char *hname,
 					struct hostent *hp);
 
 nfs_client	*clientlist[MCL_MAXTYPES] = { NULL, };
@@ -115,13 +115,18 @@ client_lookup(char *hname, int canonical)
 		clp = (nfs_client *) xmalloc(sizeof(*clp));
 		memset(clp, 0, sizeof(*clp));
 		clp->m_type = htype;
-		client_init(clp, hname, NULL);
+		if (!client_init(clp, hname, NULL)) {
+			client_free(clp);
+			clp = NULL;
+			goto out;
+		}
 		client_add(clp);
 	}
 
 	if (htype == MCL_FQDN && clp->m_naddr == 0)
 		init_addrlist(clp, hp);
 
+out:
 	if (hp)
 		free (hp);
 
@@ -138,12 +143,15 @@ client_dup(nfs_client *clp, struct hostent *hp)
 	new->m_type = MCL_FQDN;
 	new->m_hostname = NULL;
 
-	client_init(new, (char *) hp->h_name, hp);
+	if (!client_init(new, hp->h_name, hp)) {
+		client_free(new);
+		return NULL;
+	}
 	client_add(new);
 	return new;
 }
 
-static void
+static int
 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
 {
 	xfree(clp->m_hostname);
@@ -173,15 +181,17 @@ client_init(nfs_client *clp, const char *hname, struct hostent *hp)
 					htonl ((uint32_t) ~0 << (32 - netmask));
 			}
 			else {
-				xlog(L_FATAL, "invalid netmask `%s' for %s",
-				     cp + 1, clp->m_hostname);
+				xlog(L_ERROR, "invalid netmask `%s' for %s",
+					     cp + 1, clp->m_hostname);
+				return 0;
 			}
 		}
 		*cp = '/';
-		return;
+		return 1;
 	}
 	
 	init_addrlist(clp, hp);
+	return 1;
 }
 
 void
#4 lore raw

PATCH 3/4 libexport.a: Allow m_hostname allocation to fail instead of exit

In reply to a message not in the archive

Clean up: Replace xstrdup() with strdup(3) in client_init(), to prevent the process from exiting if the memory allocation fails.

Note that both of client_init()'s callers set m_hostname equal to NULL before calling, thus the extra free(3) at the top of client_init() is unneeded.

---

 support/export/client.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
support/export/client.c +4 -6
index 8f83da3..fc1ade9 100644
@@ -55,7 +55,7 @@ init_addrlist(nfs_client *clp, const struct hostent *hp)
 static void
 client_free(nfs_client *clp)
 {
-	xfree(clp->m_hostname);
+	free(clp->m_hostname);
 	xfree(clp);
 }
 
@@ -154,11 +154,9 @@ client_dup(nfs_client *clp, struct hostent *hp)
 static int
 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
 {
-	xfree(clp->m_hostname);
-	if (hp)
-		clp->m_hostname = xstrdup(hp->h_name);
-	else
-		clp->m_hostname = xstrdup(hname);
+	clp->m_hostname = strdup(hname);
+	if (clp->m_hostname == NULL)
+		return false;
 
 	clp->m_exported = 0;
 	clp->m_count = 0;
#5 lore raw

PATCH 4/4 libexport.a: Allow malloc(3) failures in client_lookup() and friends

In reply to a message not in the archive

Clean up: Use malloc(3) instead of xmalloc() in client_lookup() and client_dup(), ensuring that a failed memory allocation here doesn't cause our process to exit suddenly.

Allocation of nfs_client records and the m_hostname string are now consistently handled with malloc(3), calloc(3), strdup(3), and free(3).

---

 support/export/client.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)
support/export/client.c +9 -6
index fc1ade9..3b3a898 100644
@@ -17,7 +17,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <netdb.h>
-#include "xmalloc.h"
+
 #include "misc.h"
 #include "nfslib.h"
 #include "exportfs.h"
@@ -56,7 +56,7 @@ static void
 client_free(nfs_client *clp)
 {
 	free(clp->m_hostname);
-	xfree(clp);
+	free(clp);
 }
 
 /* if canonical is set, then we *know* this is already a canonical name
@@ -111,9 +111,10 @@ client_lookup(char *hname, int canonical)
 		}
 	}
 
-	if (!clp) {
-		clp = (nfs_client *) xmalloc(sizeof(*clp));
-		memset(clp, 0, sizeof(*clp));
+	if (clp == NULL) {
+		clp = calloc(1, sizeof(*clp));
+		if (clp == NULL)
+			goto out;
 		clp->m_type = htype;
 		if (!client_init(clp, hname, NULL)) {
 			client_free(clp);
@@ -138,7 +139,9 @@ client_dup(nfs_client *clp, struct hostent *hp)
 {
 	nfs_client		*new;
 
-	new = (nfs_client *) xmalloc(sizeof(*new));
+	new = (nfs_client *)malloc(sizeof(*new));
+	if (new == NULL)
+		return NULL;
 	memcpy(new, clp, sizeof(*new));
 	new->m_type = MCL_FQDN;
 	new->m_hostname = NULL;
Reviewed-by
#6 lore raw
In reply to a message not in the archive
Quoted 15 lines · On Mon, 19 Apr 2010 16:20:51 -0400 Chuck Lever <[email protected]> wrote:

Hi Steve-

These patches clean up some memory allocation nits in libexport.a. They pave the way for parsing IPv6 addresses and netmasks in client_init().

---

Chuck Lever (4):
libexport.a: Allow malloc(3) failures in client_lookup() and friends libexport.a: Allow m_hostname allocation to fail instead of exit libexport.a: Allow client_init() to fail instead of exit libexport.a: Add client_free()

Quoted 3 lines
 support/export/client.c |   55 +++++++++++++++++++++++++++++++----------------
 1 files changed, 36 insertions(+), 19 deletions(-)

These look good to me.

#7 lore raw
In reply to Chuck Lever
Quoted 15 lines · Chuck Lever

Hi Steve-

These patches clean up some memory allocation nits in libexport.a. They pave the way for parsing IPv6 addresses and netmasks in client_init().

---

Chuck Lever (4):
libexport.a: Allow malloc(3) failures in client_lookup() and friends libexport.a: Allow m_hostname allocation to fail instead of exit libexport.a: Allow client_init() to fail instead of exit libexport.a: Add client_free()

Quoted 3 lines
 support/export/client.c |   55 +++++++++++++++++++++++++++++++----------------
 1 files changed, 36 insertions(+), 19 deletions(-)

All four committed...

steved.