Skip to content

Commit

Permalink
fix crash when using array as function with invalid indices
Browse files Browse the repository at this point in the history
  • Loading branch information
benibela committed Jun 30, 2024
1 parent d665e78 commit 6b3b03f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 4 additions & 0 deletions data/tests/xpath3_1_tests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ procedure unittests(testerrors: boolean);
t('sort(map:remove( map {"a": 1, "b": 2, "c": 3, "d": 4}, ("b", "d", "e") ) ? *)', '1 3');
t('sort(map:for-each(map{"a": 2, "b": 3}, function($k, $v){$k ||$v}))', 'a2 b3');

t('for-each("a", map { "a": 123 } )', '123');
t('for-each(2, [ "1", 9 ])', '9');
f('for-each("2", [ "1", 9 ])', 'XPTY0004');

t('(map {}, array {}) ! (position(), . instance of map(*), . instance of map(string,string), '+
'. instance of array(*), . instance of array(string), ' +
'"F", . instance of function(*), . instance of function(integer) as item()*, . instance of function(integer) as item(), . instance of function(string) as item()*, . instance of function(string) as item() )',
Expand Down
6 changes: 3 additions & 3 deletions data/xquery.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,7 @@ TXQTermSequenceType = class(TXQTermWithChildren)
class function subtypeItemTypeAtomic(xsa, xsb: TXSType): boolean; static;
//if self is a subtype-itemtype of tb
function subtypeItemTypeOf(tb: TXQTermSequenceType): boolean;
function functionCoercion(const v: IXQValue): IXQValue;
function functionCoercion(const context: TXQStaticContext; const v: IXQValue): IXQValue;
function isItemStar(): boolean;
function isAtomicType(a: TXSType; shouldAllowNone: boolean = false; shouldAllowMultiple: boolean = false): boolean;
end;
Expand Down Expand Up @@ -7885,10 +7885,10 @@ class procedure TXQAbstractFunctionInfo.convertType(var result: IXQValue; const
for pv in temp.GetEnumeratorPtrUnsafe do begin
case pv.kind of
pvkFunction:
if context.staticContext.strictTypeChecking then seq.add(typ.functionCoercion(pv^))
if context.staticContext.strictTypeChecking then seq.add(typ.functionCoercion(context.staticContext, pv^))
else seq.add(pv^);
pvkArray, pvkObject:
seq.add(typ.functionCoercion(pv^));
seq.add(typ.functionCoercion(context.staticContext, pv^));
else
term.raiseTypeError0004('Expected function', pv^);
end;
Expand Down
3 changes: 2 additions & 1 deletion data/xquery_terms.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ begin
exit(false);
end;

function TXQTermSequenceType.functionCoercion(const v: IXQValue): IXQValue;
function TXQTermSequenceType.functionCoercion(const context: TXQStaticContext; const v: IXQValue): IXQValue;
var
fun: TXQBoxedFunction;
needCoercion: Boolean;
Expand Down Expand Up @@ -1263,6 +1263,7 @@ begin
end;
end;
//see TXQTermDefineFunction.defineDynamicPartialApplication
fresult.context.staticContext := context; //currently only used for strict type checking of array indices
fresult.body := TXQTermDynamicFunctionCall.create(TXQTermConstant.Create(v));
setlength(fresult.parameters, length(arguments) - 1);
for i := 0 to high(fresult.parameters) do begin
Expand Down

0 comments on commit 6b3b03f

Please sign in to comment.