threads / patch / 36040

patchcache_tree_find(): remove redundant check in while condition

Subject: [PATCH] cache_tree_find(): remove redundant check in while condition

## tl;dr

4 messages between Mar 3, 2014 and Mar 3, 2014. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

Michael Haggerty· Mar 3, 2014, 16:08 UTC · lore
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
A trivial little cleanup.
 cache-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to cache-tree.c +1 −1
diff --git a/cache-tree.c b/cache-tree.c
index 0bbec43..7f63c23 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -565,7 +565,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
 			return NULL;
 		it = sub->cache_tree;
 		if (slash)
-			while (*slash && *slash == '/')
+			while (*slash == '/')
 				slash++;
 		if (!slash || !*slash)
 			return it; /* prefix ended with slashes */
-- 
1.9.0
David Kastrup· Mar 3, 2014, 16:32 UTC · re: Michael Haggerty · lore

Re: [PATCH] cache_tree_find(): remove redundant check in while condition

Michael Haggerty <mhagger@alum.mit.edu> writes:
Show 20 quoted lines
> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
> ---
> A trivial little cleanup.
>
>  cache-tree.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/cache-tree.c b/cache-tree.c
> index 0bbec43..7f63c23 100644
> --- a/cache-tree.c
> +++ b/cache-tree.c
> @@ -565,7 +565,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
>  			return NULL;
>  		it = sub->cache_tree;
>  		if (slash)
> -			while (*slash && *slash == '/')
> +			while (*slash == '/')
>  				slash++;
>  		if (!slash || !*slash)
>  			return it; /* prefix ended with slashes */

That seems dragging around a NULL slash a lot. How about not checking for it multiple times?

		if (!slash)
                	return it;
		while (*slash == '/')
			slash++;
		if (!*slash)
			return it; /* prefix ended with slashes */

As a bonus, the comment appears to be actually correct. Attempting to verify its correctness by seeing whether a non-NULL slash is guaranteed to really end with slashes, we find the following code above for defining slash:

                slash = strchr(path, '/');
                if (!slash)
                        slash = path + strlen(path);

So it is literally impossible for slash to ever be NULL and all the checking is nonsensical. In addition, "prefix ended with slashes" does not seem overly convincing when this code path is reached whether or not there is a slash at all (I am not sure about it: it depends on the preceding find_subtree to some degree).

So perhaps all of that should just be
		while (*slash == '/')
			slash++;
		if (!*slash)
			return it;
-- 
David Kastrup
Michael Haggerty· Mar 3, 2014, 17:07 UTC · re: David Kastrup · lore

Re: [PATCH] cache_tree_find(): remove redundant check in while condition

On 03/03/2014 05:32 PM, David Kastrup wrote:
Show 8 quoted lines
> [...]
> So perhaps all of that should just be
> 
> 		while (*slash == '/')
> 			slash++;
> 		if (!*slash)
> 			return it;
> 

I just fixed a little thing that caught my eye. You OWNED it. You are absolutely right.

Will you prepare a patch or would you like me to do it?
Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/
David Kastrup· Mar 3, 2014, 17:25 UTC · re: Michael Haggerty · lore

Re: [PATCH] cache_tree_find(): remove redundant check in while condition

Michael Haggerty <mhagger@alum.mit.edu> writes:
Show 14 quoted lines
> On 03/03/2014 05:32 PM, David Kastrup wrote:
>> [...]
>> So perhaps all of that should just be
>> 
>> 		while (*slash == '/')
>> 			slash++;
>> 		if (!*slash)
>> 			return it;
>> 
>
> I just fixed a little thing that caught my eye.  You OWNED it.  You are
> absolutely right.
>
> Will you prepare a patch or would you like me to do it?
Feel free.  I really should be finishing some other patches instead...
-- 
David Kastrup

← back to recent threads