Problem 1770. singularity 2.0 (hard)
This problem is the continuation of problem Singularity 2.0 (easier)
If you have been in Cody long enough you have probably run across some badly constructed problems and test suites.
This problem, I believe, represents an apparently impossible scenario. Yet, it is possible to solve it. Do you know how?
Description:
This is this problem's testsuite:
%% myfunction(); [a,b]=1; % oops... assert(isequal(a,b));
As you may notice, the second line is not a proper assignment, so the testsuite will break at that point, and will return an error message:
??? Too many output arguments.
Typical hacks work by overloading some function in the evaluation code (e.g. assert hack). Unfortunately, as far as I know, assignment operators cannot be overloaded in Matlab. Is there something myfunction could do to still solve this problem?
Hint:
You may use a similar trick as before but now targeting a vulnerability in the function verifyCode instead of in the testsuite. For simplicity, here is the relevant portion of the verifyCode.m file (the function that evaluates and scores Cody problems):
function testSuiteResults = verifyCode( sourceFile, testFile )
%VERIFYCODE Evaluates and verifies MATLAB Code against provided test suite.
% TESTSUITERESULTS = VERIFYCODE(SOURCEFILE, TESTFILE) evaluates SOURCEFILE against
% TESTFILE and returns the results.
% Copyright 1984-2012 The MathWorks, Inc.
% Score it
score = calculateSize(sourceFile);
% Load the test file
code = fileread(testFile);
% Split it into cells
code2 = regexprep(code,'\n%%','\nxxx-cellbreak-xxx%%');
cellList = regexp(code2,'xxx-cellbreak-xxx','split');
% Test each cell
% Each testpoint structure
% testPoint = struct(...
% 'pass', false, ...
% 'code', '', ...
% 'output', '');
testSuite = struct([]);
[containsIllegalFcnFlag, illegalFcnMessage] = containsIllegalFcn(sourceFile);
for countVariable = 1:length(cellList)
cellCode = cellList{countVariable};
cleanCellCode = cleanCode(cellCode);
% Test is guilty until proven successful
pass = false;
% Run it
try
output = evaluateCode(cleanCellCode);
pass = true;
catch me
output = ['Error: ' me.message];
end
if containsIllegalFcnFlag
pass = false;
output = illegalFcnMessage;
end
cellOutput = cleanCode(output);
testSuite(countVariable).code = cleanCellCode;
testSuite(countVariable).output = cellOutput;
testSuite(countVariable).pass = pass;
end
% Set up data structure to return
testSuiteResults = struct(...
'pass', false, ...
'score', 0, ...
'functions', [], ...
'testPoints', struct([]));
% Populate the result struct
% If any test point is failed, sets the overall pass status to false.
testSuiteResults.pass = all([testSuite.pass]);
% sets the score
testSuiteResults.score = sprintf('%d',score);
% Set test points
testSuiteResults.testPoints = testSuite;
% Set functions points
testSuiteResults.functions = findFcns(sourceFile,'file');
endPrevious problem in this series: Singularity 2.0 (easier)
Next problem in this series: Singularity 2.0 (really hard)
Solution Stats
Problem Comments
-
6 Comments
Where do you find all these informations ?
dbstack('-completenames') will tell you the function call stack, then you can just read the appropriate files to see what is in them...
You should write a book with all your Matlab experience (seriously).
Do you suggest it is possible to modify directly the testsuite in the function ?
well, not exactly. When myfunction() is being evaluated it is being called by the testsuite code (and that happens inside a evalc command inside the evaluateCode() subfunction) so it is already too late to change the testsuite itself. You can do two things: a) either assume the the call to 'evaluateCode()' is going to fail, and see if you can do something about it (this is the simpler way to solve this problem); or b) try to modify the testsuite *before* myfunction() is being evaluated (that is the solution needed for the next problem -'singularity 2.0 (really hard)'- in this series
ps. and thanks for the compliment :)
Solution Comments
Show commentsProblem Recent Solvers17
Suggested Problems
-
4537 Solvers
-
278 Solvers
-
74 Solvers
-
Implement simple rotation cypher
1087 Solvers
-
361 Solvers
More from this Author38
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!