Skip to content

Commit 9c01e70

Browse files
authored
Do not crash when getting invalid input for setAssignment (#7570)
1 parent 95f82e5 commit 9c01e70

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

OMCompiler/Compiler/runtime/BackendDAEEXT_omc.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,26 @@ extern void BackendDAEEXT_matching(modelica_integer nv, modelica_integer ne, mod
140140
BackendDAEExtImpl__matching(nv, ne, matchingID, cheapID, relabel_period, clear_match);
141141
}
142142

143+
static void failBecauseLength(const char *function, const char *var1str, long len1, const char *var2str, long len2)
144+
{
145+
char len1str[64],len2str[64];
146+
const char *tokens[5] = {var2str,len2str,len1str,var1str,function};
147+
snprintf(len1str,64,"%ld", (long) len1);
148+
snprintf(len2str,64,"%ld", (long) len2);
149+
c_add_message(NULL,-1,ErrorType_symbolic,ErrorLevel_internal,"%s failed because %s=%s>%s=%s",tokens,5);
150+
}
151+
143152
extern void BackendDAEEXT_getAssignment(modelica_metatype ass1, modelica_metatype ass2)
144153
{
145154
int i=0;
146155
mmc_uint_t len1 = MMC_HDRSLOTS(MMC_GETHDR(ass1));
147156
mmc_uint_t len2 = MMC_HDRSLOTS(MMC_GETHDR(ass2));
148-
if (n > len1 || m > len2) {
149-
char nstr[64],mstr[64],len1str[64],len2str[64];
150-
const char *tokens[4] = {len2str,mstr,len1str,nstr};
151-
snprintf(nstr,64,"%ld", (long) n);
152-
snprintf(mstr,64,"%ld", (long) m);
153-
snprintf(len1str,64,"%ld", (long) len1);
154-
snprintf(len2str,64,"%ld", (long) len2);
155-
c_add_message(NULL,-1,ErrorType_symbolic,ErrorLevel_internal,"BackendDAEEXT.getAssignment failed because n=%s>arrayLength(ass1)=%s or m=%s>arrayLength(ass2)=%s",tokens,4);
157+
if (n > len1) {
158+
failBecauseLength("BackendDAEEXT.getAssignment", "n", n, "arrayLength(ass1)", len1);
159+
MMC_THROW();
160+
}
161+
if (m > len2) {
162+
failBecauseLength("BackendDAEEXT.getAssignment", "m", m, "arrayLength(ass2)", len2);
156163
MMC_THROW();
157164
}
158165
if (match != NULL) {
@@ -181,6 +188,10 @@ extern int BackendDAEEXT_setAssignment(int lenass1, int lenass2, modelica_metaty
181188
nelts = MMC_HDRSLOTS(MMC_GETHDR(ass1));
182189
if (nelts > 0) {
183190
n = lenass1;
191+
if (n > nelts) {
192+
failBecauseLength("BackendDAEEXT.setAssignment", "n", n, "arrayLength(ass1)", nelts);
193+
return 0;
194+
}
184195
if(match) {
185196
free(match);
186197
}
@@ -193,6 +204,10 @@ extern int BackendDAEEXT_setAssignment(int lenass1, int lenass2, modelica_metaty
193204
nelts = MMC_HDRSLOTS(MMC_GETHDR(ass2));
194205
if (nelts > 0) {
195206
m = lenass2;
207+
if (m > nelts) {
208+
failBecauseLength("BackendDAEEXT.setAssignment", "m", m, "arrayLength(ass2)", nelts);
209+
return 0;
210+
}
196211
if(row_match) {
197212
free(row_match);
198213
}

OMCompiler/Compiler/runtime/matching.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ void match_pf_fair(int* col_ptrs, int* col_ids, int* match, int* row_match, int
314314
int i, j, row, col, stack_col, temp, ptr, eptr, stack_last,
315315
stop = 0, pcount = 1, stack_end_ptr, nunmatched = 0, nextunmatched = 0,
316316
current_col, inc = 1;
317+
size_t curStackSize = n;
317318

318319
memset(visited, 0, sizeof(int) * m);
319320
memcpy(lookahead, col_ptrs, sizeof(int) * n);
@@ -353,8 +354,17 @@ void match_pf_fair(int* col_ptrs, int* col_ids, int* match, int* row_match, int
353354
continue;
354355
}
355356

356-
row = col_ids[ptr]; visited[row] = pcount;
357-
col = row_match[row]; stack[++stack_last] = col; colptrs[col] = col_ptrs[col];
357+
row = col_ids[ptr];
358+
visited[row] = pcount;
359+
col = row_match[row];
360+
if (++stack_last >= curStackSize) {
361+
stack = realloc(stack, sizeof(int)*(curStackSize*=2));
362+
}
363+
stack[stack_last] = col;
364+
if (col >= n) {
365+
fprintf(stderr, "Reading outside of array row_match[%d]=%d, n=%d, m=%d\n", row, col, n, m);
366+
}
367+
colptrs[col] = col_ptrs[col];
358368
} else {
359369
row = col_ids[ptr]; visited[row] = pcount;
360370
while(row != -1){
@@ -407,8 +417,13 @@ void match_pf_fair(int* col_ptrs, int* col_ids, int* match, int* row_match, int
407417
continue;
408418
}
409419

410-
row = col_ids[ptr]; visited[row] = pcount;
411-
col = row_match[row]; stack[++stack_last] = col;
420+
row = col_ids[ptr];
421+
visited[row] = pcount;
422+
col = row_match[row];
423+
if (++stack_last >= curStackSize) {
424+
stack = realloc(stack, sizeof(int)*(curStackSize*=2));
425+
}
426+
stack[stack_last] = col;
412427
colptrs[col] = col_ptrs[col + 1] - 1;
413428

414429
} else {

0 commit comments

Comments
 (0)