Skip to content

Commit e2efc42

Browse files
Christian BorntraegerMartin Schwidefsky
authored andcommitted
s390/lib: fix memcmp and strstr
if two string compare equal the clcle instruction will update the string addresses to point _after_ the string. This might already be on a different page, so we should not use these pointer to calculate the difference as in that case the calculation of the difference can cause oopses. The return value of memcmp does not need the difference, we can just reuse the condition code and return for CC=1 (All bytes compared, first operand low) -1 and for CC=2 (All bytes compared, first operand high) +1 strstr also does not need the diff. While fixing this, make the common function clcle "correct on its own" by using l1 instead of l2 for the first length. strstr will call this with l2 for both strings. Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com> Fixes: db7f5ee ("s390/lib: use basic blocks for inline assemblies") Reviewed-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent 134a24c commit e2efc42

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

‎arch/s390/lib/string.c‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,10 @@ char * strrchr(const char * s, int c)
237237
EXPORT_SYMBOL(strrchr);
238238

239239
static inline int clcle(const char *s1, unsigned long l1,
240-
const char *s2, unsigned long l2,
241-
int *diff)
240+
const char *s2, unsigned long l2)
242241
{
243242
register unsigned long r2 asm("2") = (unsigned long) s1;
244-
register unsigned long r3 asm("3") = (unsigned long) l2;
243+
register unsigned long r3 asm("3") = (unsigned long) l1;
245244
register unsigned long r4 asm("4") = (unsigned long) s2;
246245
register unsigned long r5 asm("5") = (unsigned long) l2;
247246
int cc;
@@ -252,7 +251,6 @@ static inline int clcle(const char *s1, unsigned long l1,
252251
" srl %0,28"
253252
: "=&d" (cc), "+a" (r2), "+a" (r3),
254253
"+a" (r4), "+a" (r5) : : "cc");
255-
*diff = *(char *)r2 - *(char *)r4;
256254
return cc;
257255
}
258256

@@ -270,9 +268,9 @@ char * strstr(const char * s1,const char * s2)
270268
return (char *) s1;
271269
l1 = __strend(s1) - s1;
272270
while (l1-- >= l2) {
273-
int cc, dummy;
271+
int cc;
274272

275-
cc = clcle(s1, l1, s2, l2, &dummy);
273+
cc = clcle(s1, l2, s2, l2);
276274
if (!cc)
277275
return (char *) s1;
278276
s1++;
@@ -313,11 +311,11 @@ EXPORT_SYMBOL(memchr);
313311
*/
314312
int memcmp(const void *cs, const void *ct, size_t n)
315313
{
316-
int ret, diff;
314+
int ret;
317315

318-
ret = clcle(cs, n, ct, n, &diff);
316+
ret = clcle(cs, n, ct, n);
319317
if (ret)
320-
ret = diff;
318+
ret = ret == 1 ? -1 : 1;
321319
return ret;
322320
}
323321
EXPORT_SYMBOL(memcmp);

0 commit comments

Comments
 (0)