Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Check that command-line arguments are UTF-8 encoded. Convert illegal UTF-8 into 7-bit ascii for the error-messages.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13382 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 15, 2012
1 parent 681192d commit d5e063a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Compiler/Util/Error.mo
Expand Up @@ -618,7 +618,8 @@ public constant Message RECURSION_DEPTH_REACHED = MESSAGE(526, TRANSLATION(), ER
Util.gettext("The maximum recursion depth was reached, probably due to mutual recursion. The current scope: %s."));
public constant Message DERIVATIVE_INPUT = MESSAGE(527, TRANSLATION(), ERROR(),
Util.gettext("The model requires derivatives of some inputs as listed below:\n%s"));

public constant Message UTF8_COMMAND_LINE_ARGS = MESSAGE(528, TRANSLATION(), ERROR(),
Util.gettext("The compiler was sent command-line arguments that were not UTF-8 encoded and will abort the current execution."));

public constant Message MATCH_SHADOWING = MESSAGE(5001, TRANSLATION(), ERROR(),
Util.gettext("Local variable '%s' shadows another variable."));
Expand Down Expand Up @@ -700,7 +701,7 @@ public constant Message OPTIMICA_ERROR = MESSAGE(7006, TRANSLATION(), ERROR(),
Util.notrans("Optimica: %s."));
public constant Message FILE_NOT_FOUND_ERROR = MESSAGE(7007, SCRIPTING(), ERROR(),
Util.gettext("File not Found: %s."));

protected import ErrorExt;

public function updateCurrentComponent "Function: updateCurrentComponent
Expand Down
5 changes: 5 additions & 0 deletions Compiler/Util/Flags.mo
Expand Up @@ -66,6 +66,7 @@ protected import List;
protected import Print;
protected import Settings;
protected import System;
public import Absyn;
public import Util;

public uniontype DebugFlag
Expand Down Expand Up @@ -949,9 +950,13 @@ public function readArgs
output list<String> outArgs;
protected
Flags flags;
Integer numError;
algorithm
numError := Error.getNumErrorMessages();
flags := loadFlags();
outArgs := List.filter1OnTrue(inArgs, readArg, flags);
_ := List.map2(outArgs,System.iconv,"UTF-8","UTF-8");
Error.assertionOrAddSourceMessage(numError == Error.getNumErrorMessages(), Error.UTF8_COMMAND_LINE_ARGS, {}, Absyn.dummyInfo);
saveFlags(flags);
end readArgs;

Expand Down
30 changes: 28 additions & 2 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -1774,6 +1774,30 @@ extern int SystemImpl__reopenStandardStream(int id,const char *filename)
return 1;
}

static char* SystemImpl__iconv__ascii(const char * str)
{
static char *buf = 0;
static int buflen = 0;
char *in_str,*res;
size_t sz,out_sz;
iconv_t ic;
int i;
sz = strlen(str);
if (buflen < sz) {
if (buf) free(buf);
buf = (char*)malloc(sz);
if (!buf) {
buflen = 0;
return (char*) "";
}
buflen = sz;
}
*buf = 0;
for (i=0; i<sz; i++)
buf[i] = str[i] & 0x80 ? '?' : str[i];
return buf;
}

extern char* SystemImpl__iconv(const char * str, const char *from, const char *to, int printError)
{
static char *buf = 0;
Expand All @@ -1796,7 +1820,8 @@ extern char* SystemImpl__iconv(const char * str, const char *from, const char *t
/* fprintf(stderr,"iconv(%s,to=%s,%s) of size %d, buflen %d\n",str,to,from,sz,buflen); */
ic = iconv_open(to, from);
if (ic == (iconv_t) -1) {
const char *tokens[4] = {strerror(errno),from,to,str};
char *ignore = SystemImpl__iconv__ascii(str);
const char *tokens[4] = {strerror(errno),from,to,ignore};
if (printError) c_add_message(-1,ErrorType_scripting,ErrorLevel_error,gettext("iconv(\"%s\",to=\"%s\",from=\"%s\") failed: %s"),tokens,4);
return (char*) "";
}
Expand All @@ -1806,7 +1831,8 @@ extern char* SystemImpl__iconv(const char * str, const char *from, const char *t
count = iconv(ic,&in_str,&sz,&res,&out_sz);
iconv_close(ic);
if (count == -1) {
const char *tokens[4] = {strerror(errno),from,to,str};
char *ignore = SystemImpl__iconv__ascii(str);
const char *tokens[4] = {strerror(errno),from,to,ignore};
if (printError) c_add_message(-1,ErrorType_scripting,ErrorLevel_error,gettext("iconv(\"%s\",to=\"%s\",from=\"%s\") failed: %s"),tokens,4);
return (char*) "";
}
Expand Down

0 comments on commit d5e063a

Please sign in to comment.