Skip to contentSkip to conversation

fix string comparison to do full matches

PATCH In review Jeff Moyer 2 participants 2 messages Nov 3, 2006 Kernel automounter

lore
Share & tools
lore

Jeff Moyer

author
#0 lore raw

PATCH 1/1 fix string comparison to do full matches

Hi, Ian, list,

	while(1) {
		entry = read_one(f, mkey, mapent);
		if (entry)
			if (strncmp(mkey, key, key_len) == 0) {
				fclose(f);
				return cache_update(root, key, mapent, age);
			}

		if (feof(f))
			break;
	}

The strncmp above will match a partial key in the cache. What I mean by that is, if we are looking up "foo", and there is an entry for "foobar", that code would incorrectly update the "foobar" entry with the value for the key "foo". The fix is simple, and has been in the RHEL 4 code for some time.

I tested it as follows:

---

Setup the automounter to mount a local map (nsswitch should have 'automount: files')

The auto.master file should have the following entry:
/misc /etc/auto.misc --timeout=3

The /etc/auto.misc file should have the following entries:
foo2 mountpoint1
foo mountpoint2

where mountpoint1 & mountpoint2 are two different mountpoints (ie. tng4-1:/net/1/1/1 & tng4-1:/net/1/1/2)

Run 'ls /misc/foo ; ls /misc/foo2'

You should get a list of the files in both mountpoints. Wait 10 seconds. Run df and make sure both dirs were unmounted.

touch /etc/auto.misc

Run 'ls /misc/foo ; ls /misc/foo2'

You should have the same output as the first 'ls /misc/foo ; ls /misc/foo2'

---

This patch is again from Chris Feist. Comments welcome.

-Jeff

modules/lookup_file.c +2 -1
index 2eb002e..49d1155 100644
@@ -345,7 +345,8 @@ static int lookup_one(const char *root,
 	while(1) {
 		entry = read_one(f, mkey, mapent);
 		if (entry)
-			if (strncmp(mkey, key, key_len) == 0) {
+			if (strlen(mkey) == key_len &&
+			    strncmp(mkey, key, key_len) == 0) {
 				fclose(f);
 				return cache_update(root, key, mapent, age);
 			}
#1 lore raw

modules/lookup_file.c static int lookup_one(const char *root, · line 349

Quoted 5 lines · Jeff Moyer

This patch is again from Chris Feist. Comments welcome.

modules/lookup_file.c +2 -0
+			if (strlen(mkey) == key_len &&
+			    strncmp(mkey, key, key_len) == 0) {

If this was a Google interview, I'd say that doing the strncmp before the strlen is more efficient. With equal keys it does not matter, but with different keys strncmp will bail out in the middle of the key, on average, and strlen will always scan the whole key. So doing strlen and then strncmp scans 1.5 the length of the key on average, while doing strncmp and then strlen scans 0.5 of it (with different keys, remember).

Other than that, the patch seems to be the right thing to do. :)

Fábio

Signature · 1 line
ex sed lex awk yacc, e pluribus unix, amem