Skip to content

Commit aefddb4

Browse files
committed
bugfix: sf.net #899 trim( wstring ) causes crash if string is single space
- avoid returning a pointer before the first element in fb_wstr_SkipCharRev(), &s[-1] is undefined behaviour - also then avoids passing start > end pointers to fb_wstr_CalcDiff() - fb_wstr_SkipCharRev() now returns a pointer to the start of all skipped chars, or the end marker if no chars are skipped
1 parent 501d2e7 commit aefddb4

File tree

6 files changed

+10
-10
lines changed

6 files changed

+10
-10
lines changed

src/rtlib/fb_unicode.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,12 @@ static __inline__ const FB_WCHAR *fb_wstr_SkipCharRev( const FB_WCHAR *s, ssize_
220220
return s;
221221

222222
/* fixed-len's are filled with null's as in PB, strip them too */
223-
const FB_WCHAR *p = &s[chars-1];
223+
const FB_WCHAR *p = &s[chars];
224224
while( chars > 0 )
225225
{
226-
if( *p != c )
227-
return p;
228226
--p;
227+
if( *p != c )
228+
return ++p;
229229
--chars;
230230
}
231231

src/rtlib/strw_ltrimex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ FBCALL FB_WCHAR *fb_WstrLTrimEx ( const FB_WCHAR *src, const FB_WCHAR *pattern )
2020
p = fb_wstr_SkipChar( src,
2121
len,
2222
*pattern );
23-
len = len - (ssize_t)(p - src);
23+
len -= fb_wstr_CalcDiff( src, p );
2424

2525
} else if( len_pattern != 0 ) {
2626
p = src;

src/rtlib/strw_rtrim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ FBCALL FB_WCHAR *fb_WstrRTrim ( const FB_WCHAR *src )
1616
return NULL;
1717

1818
p = fb_wstr_SkipCharRev( src, chars, _LC(' ') );
19-
chars = fb_wstr_CalcDiff( src, p ) + 1;
19+
chars = fb_wstr_CalcDiff( src, p );
2020
if( chars <= 0 )
2121
return NULL;
2222

src/rtlib/strw_rtrimex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ FBCALL FB_WCHAR *fb_WstrRTrimEx ( const FB_WCHAR *src, const FB_WCHAR *pattern )
2121
p = fb_wstr_SkipCharRev( src,
2222
len,
2323
*pattern );
24-
len = (ssize_t)(p - src) + 1;
24+
len = fb_wstr_CalcDiff( src, p );
2525

2626
} else if( len_pattern != 0 ) {
2727
ssize_t test_index = len - len_pattern;

src/rtlib/strw_trim.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ FBCALL FB_WCHAR *fb_WstrTrim ( const FB_WCHAR *src )
1616
return NULL;
1717

1818
p = fb_wstr_SkipCharRev( src, chars, _LC(' ') );
19-
chars = fb_wstr_CalcDiff( src, p ) + 1;
19+
chars = fb_wstr_CalcDiff( src, p );
2020
if( chars <= 0 )
2121
return NULL;
2222

@@ -26,7 +26,7 @@ FBCALL FB_WCHAR *fb_WstrTrim ( const FB_WCHAR *src )
2626
return NULL;
2727

2828
/* alloc temp string */
29-
dst = fb_wstr_AllocTemp( chars );
29+
dst = fb_wstr_AllocTemp( chars );
3030
if( dst != NULL )
3131
{
3232
/* simple copy */

src/rtlib/strw_trimex.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ FBCALL FB_WCHAR *fb_WstrTrimEx ( const FB_WCHAR *src, const FB_WCHAR *pattern )
2020
p = fb_wstr_SkipChar( src,
2121
len,
2222
*pattern );
23-
len = len - (ssize_t)(p - src);
23+
len -= fb_wstr_CalcDiff( src, p );
2424

2525
} else if( len_pattern != 0 ) {
2626
p = src;
@@ -38,7 +38,7 @@ FBCALL FB_WCHAR *fb_WstrTrimEx ( const FB_WCHAR *src, const FB_WCHAR *pattern )
3838
fb_wstr_SkipCharRev( p,
3939
len,
4040
*pattern );
41-
len = (ssize_t)(p_tmp - p) + 1;
41+
len = fb_wstr_CalcDiff( p, p_tmp );
4242

4343
} else if( len_pattern != 0 ) {
4444
ssize_t test_index = len - len_pattern;

0 commit comments

Comments
 (0)