Skip to content
Snippets Groups Projects
Commit 283b7947 authored by Simon McVittie's avatar Simon McVittie
Browse files

pv_envp_cmp: Sort environment variables in the intended order


While adding basic test coverage I discovered that this was not giving
the intended answer if two values for the same variable appear in the
environment block, which is rare but technically possible.

Signed-off-by: default avatarSimon McVittie <smcv@collabora.com>
parent bcb942eb
No related branches found
No related tags found
No related merge requests found
...@@ -94,22 +94,35 @@ pv_envp_cmp (const void *p1, ...@@ -94,22 +94,35 @@ pv_envp_cmp (const void *p1,
min = MIN (l1, l2); min = MIN (l1, l2);
ret = strncmp (*s1, *s2, min); ret = strncmp (*s1, *s2, min);
/* If they differ before the first '=' (if any) in either s1 or s2,
* then they are certainly different */
if (ret != 0) if (ret != 0)
return ret; return ret;
ret = strcmp (*s1, *s2);
/* If they do not differ at all, then they are equal */
if (ret == 0)
return ret;
/* FOO < FOO=..., and FOO < FOOBAR */
if ((*s1)[min] == '\0') if ((*s1)[min] == '\0')
return -1; return -1;
/* FOO=... > FOO, and FOOBAR > FOO */
if ((*s2)[min] == '\0') if ((*s2)[min] == '\0')
return 1; return 1;
if ((*s1)[min] == '=') /* FOO= < FOOBAR */
if ((*s1)[min] == '=' && (*s2)[min] != '=')
return -1; return -1;
if ((*s2)[min] == '=') /* FOOBAR > FOO= */
if ((*s2)[min] == '=' && (*s1)[min] != '=')
return 1; return 1;
return strcmp (*s1, *s2); /* Fall back to plain string comparison */
return ret;
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment