Fix type coercion from empty string pointer to 0

This commit is contained in:
drmortalwombat 2023-08-08 17:33:47 +02:00
parent b7630450f1
commit 07969d1fa6
3 changed files with 58 additions and 35 deletions

View File

@ -33,12 +33,17 @@ string::string(const string & s)
string::string(const char * s) string::string(const char * s)
{ {
char l = sstrlen(s); if (s)
if (l)
{ {
cstr = malloc(char(l + 2)); char l = sstrlen(s);
cstr[0] = l; if (l)
smemcpy(cstr + 1, s, l + 1); {
cstr = malloc(char(l + 2));
cstr[0] = l;
smemcpy(cstr + 1, s, l + 1);
}
else
cstr = nullptr;
} }
else else
cstr = nullptr; cstr = nullptr;
@ -102,12 +107,17 @@ string & string::operator=(const string & s)
string & string::operator=(const char * s) string & string::operator=(const char * s)
{ {
free(cstr); free(cstr);
char l = sstrlen(s); if (s)
if (l)
{ {
cstr = malloc(char(l + 2)); char l = sstrlen(s);
cstr[0] = l; if (l)
smemcpy(cstr + 1, s, l + 1); {
cstr = malloc(char(l + 2));
cstr[0] = l;
smemcpy(cstr + 1, s, l + 1);
}
else
cstr = nullptr;
} }
else else
cstr = nullptr; cstr = nullptr;
@ -141,24 +151,27 @@ string & string::operator+=(const string & s)
string & string::operator+=(const char * s) string & string::operator+=(const char * s)
{ {
char sl = sstrlen(s); if (s)
if (sl)
{ {
if (cstr) char sl = sstrlen(s);
if (sl)
{ {
char l = sl + cstr[0]; if (cstr)
char * c = malloc(char(l + 2)); {
c[0] = l; char l = sl + cstr[0];
smemcpy(c + 1, cstr + 1, cstr[0]); char * c = malloc(char(l + 2));
smemcpy(c + 1 + cstr[0], s, sl + 1); c[0] = l;
free(cstr); smemcpy(c + 1, cstr + 1, cstr[0]);
cstr = c; smemcpy(c + 1 + cstr[0], s, sl + 1);
} free(cstr);
else cstr = c;
{ }
cstr = malloc(char(sl + 2)); else
cstr[0] = sl; {
smemcpy(cstr + 1, s, sl + 1); cstr = malloc(char(sl + 2));
cstr[0] = sl;
smemcpy(cstr + 1, s, sl + 1);
}
} }
} }
return *this; return *this;
@ -226,14 +239,19 @@ string string::operator+(const char * s) const
{ {
if (cstr) if (cstr)
{ {
char sl = sstrlen(s); if (s)
if (sl)
{ {
char l = cstr[0] + sl; char sl = sstrlen(s);
char * p = malloc(char(l + 2)); if (sl)
smemcpy(p + 1, cstr + 1, cstr[0]); {
smemcpy(p + 1 + cstr[0], s, sl); char l = cstr[0] + sl;
return string(l, p); char * p = malloc(char(l + 2));
smemcpy(p + 1, cstr + 1, cstr[0]);
smemcpy(p + 1 + cstr[0], s, sl);
return string(l, p);
}
else
return *this;
} }
else else
return *this; return *this;

View File

@ -16736,7 +16736,7 @@ void InterCodeProcedure::Close(void)
{ {
GrowingTypeArray tstack(IT_NONE); GrowingTypeArray tstack(IT_NONE);
CheckFunc = !strcmp(mIdent->mString, "MenuItem::+MenuItem"); CheckFunc = !strcmp(mIdent->mString, "main");
mEntryBlock = mBlocks[0]; mEntryBlock = mBlocks[0];

View File

@ -150,6 +150,11 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p
v.mTemp = cins->mDst.mTemp; v.mTemp = cins->mDst.mTemp;
v.mType = type; v.mType = type;
} }
else if (type->mType == DT_TYPE_POINTER && v.mType->mType == DT_TYPE_ARRAY)
{
v.mType = type;
return v;
}
else if (v.mType->mSize < type->mSize) else if (v.mType->mSize < type->mSize)
{ {
if (v.mType->mSize == 1 && type->mSize == 2) if (v.mType->mSize == 1 && type->mSize == 2)