Home » U++ Library support » U++ Core » [FIXED] ToUpper, ToLower,and ToAscii (char*, int) causes AssertFailed
[FIXED] ToUpper, ToLower,and ToAscii (char*, int) causes AssertFailed [message #46583] |
Fri, 03 June 2016 13:21  |
omari
Messages: 276 Registered: March 2010
|
Experienced Member |
|
|
Hi,
the following code causes Assertion fail:
#include <Core/Core.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
char* s = "abcd";
ToUpper(s, 4);
}
the backtrace:
7fefd4531f3 (KERNELBASE.dll)
Upp::AssertFailed(file=13f22b238 "c:\\upp\\uppsrc\\core\\Vcont.h", line=84 'T', cond=13f22b570 "i >= 0 && i < items") at c:\upp\uppsrc\core\util.cpp 166
Upp::Vector<Upp::CharSetData * __ptr64>::Get(i=255) at c:\upp\uppsrc\core\vcont.h 84
Upp::Vector<Upp::CharSetData * __ptr64>::operator[](i=255) at c:\upp\uppsrc\core\vcont.h 100
Upp::Array<Upp::CharSetData>::Get(i=255) at c:\upp\uppsrc\core\vcont.h 229
Upp::Array<Upp::CharSetData>::operator[](i=255) at c:\upp\uppsrc\core\vcont.h 244
Upp::AMap<Upp::String,Upp::CharSetData,Upp::Array<Upp::CharSetData> >::operator[](i=255) at c:\upp\uppsrc\core\map.h 101
Upp::s_cset(charset=255) at c:\upp\uppsrc\core\charset.cpp 2149
Upp::ToUpper(t=13f212a50 "abcd", s=13f212a50 "abcd", len=4, charset=255) at c:\upp\uppsrc\core\charset.cpp 2633
Upp::ToUpper(s=13f212a50 "abcd", len=4, charset=0) at c:\upp\uppsrc\core\charset.cpp 2660
ConsoleMainFn_() at c:\upp\myapps\consol\consol.cpp 7
Upp::AppExecute__(app=13f0288c3) at c:\upp\uppsrc\core\app.cpp 326
main(argc=1, argv=1524bc0 "R\001") at c:\upp\myapps\consol\consol.cpp 3
invoke_main()
__scrt_common_main_seh()
__scrt_common_main()
mainCRTStartup()
774459bd (kernel32.dll)
regards
omari.
[Updated on: Sat, 18 March 2017 16:00] Report message to a moderator
|
|
|
Re: [BUG] ToUpper, ToLower,and ToAscii (char*, int) causes AssertFailed [message #46591 is a reply to message #46583] |
Sun, 05 June 2016 18:15   |
 |
mirek
Messages: 14257 Registered: November 2005
|
Ultimate Member |
|
|
There are two reasons why this crashes:
a) default encoding is UTF8 and with UTF8 conversion in general does not guarantee that the ouput number of bytes is the same as input one. Now in this case, it is, but I do not think it is even healthy to support this conversion. You can make it work by adding some 8-bit charset, like
ToUpper(s, 4, CHARSET_WIN1252);
b) you are overwriting string literal, which is undefined behaviour. So
#include <Core/Core.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
char s[] = "abcd";
ToUpper(s, 4, CHARSET_WIN1250);
}
Mirek
|
|
|
Re: [BUG] ToUpper, ToLower,and ToAscii (char*, int) causes AssertFailed [message #46595 is a reply to message #46591] |
Mon, 06 June 2016 12:56   |
omari
Messages: 276 Registered: March 2010
|
Experienced Member |
|
|
Yes, there is tow,
a - the first can be avoided by specifying a charset,
a possible solution to this is to test if charset is equal to UTF8 (like others ToUpper functions), and do not call s_cset(charset) in this case.
replace : (in file CharSet.cpp, line ~ 2660)
void ToUpper(char *t, const char *s, int len, byte charset)
{
charset = ResolveCharset(charset);
CharSetData& cs = s_cset(charset);
const char *lim = s + len;
while(s < lim)
*t++ = cs.FromUnicode(UPP::ToUpper(cs.ToUnicode(*s++)));
}
by:
void ToUpper(char *t, const char *s, int len, byte charset)
{
charset = ResolveCharset(charset);
if(charset == CHARSET_UTF8)
{
String u = ToUtf8(ToUpper(FromUtf8(s, len)));
memcpy(t, ~u, len);
return;
}
CharSetData& cs = s_cset(charset);
const char *lim = s + len;
while(s < lim)
*t++ = cs.FromUnicode(UPP::ToUpper(cs.ToUnicode(*s++)));
}
b - here a test case for the second BUG:
the code below produce EXCEPTION_ACCESS_VIOLATION
CONSOLE_APP_MAIN
{
char* s = "abcd";
ToUpper(s, s, 4, CHARSET_WIN1250);
}
but the code below work as expected:
CONSOLE_APP_MAIN
{
char* s = "abcd";
StringBuffer r(4);
ToUpper(r, s, 4, CHARSET_WIN1250);
}
then, the bug can be resolved by replacing : (in file CharSet.cpp, line ~ 2630)
void ToUpper(char *s, int len, byte charset)
{
ToUpper(s, s, len, charset);
}
by
void ToUpper(char *s, int len, byte charset)
{
StringBuffer r(len);
ToUpper(r, s, len, charset);
memcpy(s, r, len);
}
regards
omari.
[Updated on: Mon, 06 June 2016 13:21] Report message to a moderator
|
|
|
|
|
Goto Forum:
Current Time: Sun May 11 14:36:46 CEST 2025
Total time taken to generate the page: 0.00641 seconds
|