From 8f8ec2adcc8e5f9ac7cb9abbd5f2fd840bcbed3b Mon Sep 17 00:00:00 2001 From: Jason Edmeades Date: Fri, 19 Aug 2005 10:01:42 +0000 Subject: [PATCH] _swab failed if src == dest, plus some testcases. --- dlls/msvcrt/string.c | 8 ++++--- dlls/msvcrt/tests/string.c | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/dlls/msvcrt/string.c b/dlls/msvcrt/string.c index e21e77654cd..478742e8bf3 100644 --- a/dlls/msvcrt/string.c +++ b/dlls/msvcrt/string.c @@ -106,9 +106,11 @@ void MSVCRT__swab(char* src, char* dst, int len) len = (unsigned)len >> 1; while (len--) { - *dst++ = src[1]; - *dst++ = *src++; - src++; + char s0 = src[0]; + char s1 = src[1]; + *dst++ = s1; + *dst++ = s0; + src = src + 2; } } } diff --git a/dlls/msvcrt/tests/string.c b/dlls/msvcrt/tests/string.c index 8449610197e..a546caa360c 100644 --- a/dlls/msvcrt/tests/string.c +++ b/dlls/msvcrt/tests/string.c @@ -29,6 +29,50 @@ static int* (*pmemcmp)(void *, const void *, size_t n); #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y) #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y) +static void test_swab( void ) { + char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#"; + char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#"; + char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$"; + char expected3[] = "$"; + + char from[30]; + char to[30]; + + int testsize; + + /* Test 1 - normal even case */ + memset(to,'$', sizeof(to)); + memset(from,'@', sizeof(from)); + testsize = 26; + memcpy(from, original, testsize); + _swab( from, to, testsize ); + ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to); + + /* Test 2 - uneven case */ + memset(to,'$', sizeof(to)); + memset(from,'@', sizeof(from)); + testsize = 25; + memcpy(from, original, testsize); + _swab( from, to, testsize ); + ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to); + + /* Test 3 - from = to */ + memset(to,'$', sizeof(to)); + memset(from,'@', sizeof(from)); + testsize = 26; + memcpy(to, original, testsize); + _swab( to, to, testsize ); + ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to); + + /* Test 4 - 1 bytes */ + memset(to,'$', sizeof(to)); + memset(from,'@', sizeof(from)); + testsize = 1; + memcpy(from, original, testsize); + _swab( from, to, testsize ); + ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to); +} + START_TEST(string) { @@ -48,4 +92,7 @@ START_TEST(string) pmemcpy((char*)mem+5, mem,nLen+1); ok(pmemcmp((char*)mem+5,xilstring, nLen) == 0, "Got result %s\n",(char*)mem+5); + + /* Test _swab function */ + test_swab(); }