Skip to content

Commit

Permalink
wstr conversion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrm committed Oct 3, 2021
1 parent c2f08a6 commit e151d9a
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions src/rtlib/strw_convfrom_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

#include "fb.h"

static ssize_t fb_wstr_ConvFromA_nomultibyte(FB_WCHAR *dst, ssize_t dst_chars, const char *src)
{
/* mbstowcs() failed; translate at least ASCII chars
and write out '?' for the others */
FB_WCHAR *origdst = dst;
FB_WCHAR *dstlimit = dst + dst_chars;
while (dst < dstlimit) {
unsigned char c = *src++;
if (c == 0)
break;
if (c > 127)
c = '?';
*dst++ = c;
}
*dst = _LC('\0');
return dst - origdst;
}

/* dst_chars == room in dst buffer without null terminator. Thus, the dst buffer
must be at least (dst_chars + 1) * sizeof(FB_WCHAR).
src must be null-terminated.
Expand Down Expand Up @@ -37,19 +55,10 @@ ssize_t fb_wstr_ConvFromA(FB_WCHAR *dst, ssize_t dst_chars, const char *src)
}

/* mbstowcs() failed; translate at least ASCII chars
and write out '?' for the others */
FB_WCHAR *origdst = dst;
FB_WCHAR *dstlimit = dst + dst_chars;
while (dst < dstlimit) {
unsigned char c = *src++;
if (c == 0)
break;
if (c > 127)
c = '?';
*dst++ = c;
}
*dst = _LC('\0');
return dst - origdst;
** and write out '?' for the others
*/
return fb_wstr_ConvFromA_nomultibyte( dst, dst_chars, src );

#endif
}

Expand All @@ -67,15 +76,23 @@ FBCALL FB_WCHAR *fb_StrToWstr( const char *src )
chars = strlen( src );
#else
chars = mbstowcs( NULL, src, 0 );

/* invalid multibyte characters? get the plain old NUL terminated
** string length and allocate a buffer for at least the ASCII chars
*/
if( chars < 0 ) {
chars = strlen( src );
}

#endif
if( chars == 0 )
return NULL;
if( chars == 0 )
return NULL;

dst = fb_wstr_AllocTemp( chars );
if( dst == NULL )
return NULL;
dst = fb_wstr_AllocTemp( chars );
if( dst == NULL )
return NULL;

fb_wstr_ConvFromA( dst, chars, src );
fb_wstr_ConvFromA( dst, chars, src );

return dst;
return dst;
}

0 comments on commit e151d9a

Please sign in to comment.