alliedmodders / sourcepawn

A small, statically typed scripting language.
Other
369 stars 63 forks source link

String return causes the compiler to crash #940

Closed Vinillia closed 9 months ago

Vinillia commented 9 months ago

SourcePawn Engine: 1.12.0.7102, jit-x86 (build 1.12.0.7102)

Code to reproduce crash:

#include <sourcemod>

char[] test(bool ok)
{
    return ok ? "ok" : "not_ok"
}
NotnHeavy commented 9 months ago

Seems like the semantics analyser assumed the given expression was an array, due to the identifier of the second node of TernaryExpr being iREFARRAY, so it ran ::CheckArrayReturnStmt() and it errors from there due to nullptr dereferencing later on.

I decided to make a fix for this because why not - will be making a pull request from my fork soon.

dvander commented 9 months ago

This is fixable with a very tiny change to CheckReturnArrayExpr, in that we can swap "sub" for "val" and it mostly just works. val carries array information, except... for enum structs. which aren't covered in the test suite for this use case, but should be.

Adding a "semantic_type" field to value would fix this, but maybe it's time to say goodbye to the "enum structs are desugared arrays" hack.

dvander commented 9 months ago

Thanks for the report, this single-handedly motivated a really important refactoring. I folded the fix into PR #945.