[PATCH] ctdb: strip trailing spaces from nodes file lines

Michael Adam obnox at samba.org
Fri Jun 26 16:54:42 MDT 2015


On 2015-06-26 at 21:46 +0200, Volker Lendecke wrote:
> On Fri, Jun 26, 2015 at 05:26:04PM +0200, Michael Adam wrote:
> > FYI: Volker pointed out to me that we have trim_string()
> > which I might re-use. I am currently evaluating that,
> > also looking for a function that can trim chars of
> > a set and not a single char or whole string, but I have
> > not yet found one. :)
> 
> strtok_r?

Not sure if that really applies:

This use case wants to advance the start pointer
of the string over leading white space and trim
trailing whitespace, not necessarily chop up the
whole string by whitespaces.

It may be true that the current use case has
no white spaces that are not leading or trailing,
but somehow this seems non-obvious to me.

About using trim_string():

One can achieve the desired effect by
two calls to trim_string:

trim_string(node, " ", " ");
trim_string(node, "\t", "\t");

This reads more nicely but has the drawback
that it uses memmove instead of just incrementing
the node pointer. But reading the nodes file is
arguably not a very performance critical operation.. ;)

So attached is the alternative patch.
Also created a bug and added the reference in the
commit message.

Thanks for further review.

Michael
-------------- next part --------------
From da4b9a6c98668d33f072af0ec0394cadeee8760a Mon Sep 17 00:00:00 2001
From: Michael Adam <obnox at samba.org>
Date: Sat, 27 Jun 2015 00:50:15 +0200
Subject: [PATCH] ctdb: strip trailing spaces from nodes file.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=11365

Signed-off-by: Michael Adam <obnox at samba.org>
---
 ctdb/common/ctdb_util.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/ctdb/common/ctdb_util.c b/ctdb/common/ctdb_util.c
index 5d63c27..fc386bc 100644
--- a/ctdb/common/ctdb_util.c
+++ b/ctdb/common/ctdb_util.c
@@ -548,14 +548,15 @@ struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
 	}
 
 	for (i=0; i < nlines; i++) {
-		const char *node;
+		char *node;
 		uint32_t flags;
 
 		node = lines[i];
-		/* strip leading spaces */
-		while((*node == ' ') || (*node == '\t')) {
-			node++;
-		}
+
+		/* strip leading and trailing spaces */
+		(void)trim_string(node, " ", " ");
+		(void)trim_string(node, "\t", "\t");
+
 		if (strcmp(node, "") == 0) {
 			continue;
 		}
@@ -566,7 +567,7 @@ struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
 			   would cause subsequent nodes to change
 			   their PNN. */
 			flags = NODE_FLAGS_DELETED;
-			node = "0.0.0.0";
+			node = discard_const("0.0.0.0");
 		} else {
 			flags = 0;
 		}
-- 
2.4.3

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20150627/a4eb9d45/attachment.pgp>


More information about the samba-technical mailing list