Skip to content

Commit

Permalink
- Avoid segfault on UTF-8 error
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11743 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Apr 13, 2012
1 parent 26432f3 commit ac26004
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Parser/BaseModelica_Lexer.g
Expand Up @@ -300,15 +300,26 @@ STRING : '"' STRING_GUTS '"'
pANTLR3_STRING text = $STRING_GUTS.text;
if (*text->chars && !*SystemImpl__iconv((const char*)text->chars,ModelicaParser_encoding,"UTF-8",0)) {
const char *strs[2];
int len = strlen((char*)$text->chars), i;
signed char *buf = (signed char*) strdup((char*)text->chars);
int len = strlen((const char*)buf), i;
/* Avoid printing huge strings */
if (len > 75) {
len = 75;
buf[len] = 0;
buf[len-1] = '.';
buf[len-2] = '.';
buf[len-3] = '.';
}
for (i=0;i<len;i++) {
if (buf[i] < 0)
buf[i] = '?';
/* Don't break lines in the printed error-message */
if (buf[i] == '\n' || buf[i] == '\r')
buf[i] = ' ';
}
strs[0] = (const char*) buf;
strs[1] = ModelicaParser_encoding;
c_add_source_message(2, ErrorType_syntax, ErrorLevel_error, "The file was not encoded in \%s: \"\%s\".\n"
c_add_source_message(2, ErrorType_syntax, ErrorLevel_error, "The file was not encoded in \%s:\n \"\%s\".\n"
" To change encoding when loading a file: loadFile(encoding=\"ISO-XXXX-YY\").\n"
" To change it in a package: add a file package.encoding at the top-level.",
strs, 2, $line, $pos+1, $line, $pos+len+1,
Expand Down

0 comments on commit ac26004

Please sign in to comment.