MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

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'); end

Previous problem in this series: Singularity 2.0 (easier)

Next problem in this series: Singularity 2.0 (really hard)

16 correct solutions
29 incorrect solutions

Last solution submitted on Apr 06, 2015

2 players like this problem

1 Comment

Jean-Marie SAINTHILLIER
on 4 Aug 2013

Correct solution with a fail !
:)

6 Comments