This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/11120] strncmp() will be broken if used in ld.so on x86-64


http://sourceware.org/bugzilla/show_bug.cgi?id=11120

--- Comment #12 from Dmitry V. Levin <ldv at altlinux dot org> 2013-03-22 23:00:44 UTC ---
(In reply to comment #11)
> There is absolutely no disagreement or question there.
> After your latest change, I observe that any attempt to call strncmp directly,
> or indirectly (e.g. by attempting to call getenv), fails at compile time.
> That's a very good thing.  Thank you.
> 
> However, you have not answered my question.
> 
> My question is about the fact that,
> contrary to what both you and Mark Seaborn have said,
> prior to your change,
> strncmp was *not* acting like strcmp, at least not consistently.
> On one call it acted like strcmp, on the next call
> (with exactly the same effective input) it didn't.

If you have a look at the implementation, you'll see that it's all the same in
the non-libc case no matter by what name it is called.
The reason why you can see different results is that gcc is capable to optimize
out some strncmp calls.  For example, in your case

$ echo -e '#include <string.h>\nint foo(void){return strncmp("abc", "ab", 2);}'
|gcc -O2 -E - |tail -1
int foo(void){return (__extension__ (__builtin_constant_p (2) &&
((__builtin_constant_p ("abc") && strlen ("abc") < ((size_t) (2))) ||
(__builtin_constant_p ("ab") && strlen ("ab") < ((size_t) (2)))) ?
__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p ("abc") &&
__builtin_constant_p ("ab") && (__s1_len = strlen ("abc"), __s2_len = strlen
("ab"), (!((size_t)(const void *)(("abc") + 1) - (size_t)(const void *)("abc")
== 1) || __s1_len >= 4) && (!((size_t)(const void *)(("ab") + 1) -
(size_t)(const void *)("ab") == 1) || __s2_len >= 4)) ? __builtin_strcmp
("abc", "ab") : (__builtin_constant_p ("abc") && ((size_t)(const void
*)(("abc") + 1) - (size_t)(const void *)("abc") == 1) && (__s1_len = strlen
("abc"), __s1_len < 4) ? (__builtin_constant_p ("ab") && ((size_t)(const void
*)(("ab") + 1) - (size_t)(const void *)("ab") == 1) ? __builtin_strcmp ("abc",
"ab") : (__extension__ ({ const unsigned char *__s2 = (const unsigned char *)
(const char *) ("ab"); register int __result = (((const unsigned char *) (const
char *) ("abc"))[0] - __s2[0]); if (__s1_len > 0 && __result == 0) { __result =
(((const unsigned char *) (const char *) ("abc"))[1] - __s2[1]); if (__s1_len >
1 && __result == 0) { __result = (((const unsigned char *) (const char *)
("abc"))[2] - __s2[2]); if (__s1_len > 2 && __result == 0) __result = (((const
unsigned char *) (const char *) ("abc"))[3] - __s2[3]); } } __result; }))) :
(__builtin_constant_p ("ab") && ((size_t)(const void *)(("ab") + 1) -
(size_t)(const void *)("ab") == 1) && (__s2_len = strlen ("ab"), __s2_len < 4)
? (__builtin_constant_p ("abc") && ((size_t)(const void *)(("abc") + 1) -
(size_t)(const void *)("abc") == 1) ? __builtin_strcmp ("abc", "ab") :
(__extension__ ({ const unsigned char *__s1 = (const unsigned char *) (const
char *) ("abc"); register int __result = __s1[0] - ((const unsigned char *)
(const char *) ("ab"))[0]; if (__s2_len > 0 && __result == 0) { __result =
(__s1[1] - ((const unsigned char *) (const char *) ("ab"))[1]); if (__s2_len >
1 && __result == 0) { __result = (__s1[2] - ((const unsigned char *) (const
char *) ("ab"))[2]); if (__s2_len > 2 && __result == 0) __result = (__s1[3] -
((const unsigned char *) (const char *) ("ab"))[3]); } } __result; }))) :
__builtin_strcmp ("abc", "ab")))); }) : strncmp ("abc", "ab", 2)));}

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]