May 8, 2020

Taking advantage of output dumping for a Gradescope programming assignment

An old story I've been saving up.

So I submitted my CS 221 Intro to Data Structures and Algorithms programming assignment and found something very peculiar. I couldn't figure out why one of the tests were failing. The test in question was one of those private tests that students don't have access to, to prevent students from simply outputting the solution when given a certain input. And to that, I was also pretty sure that I had followed the assignment to the tee. And thus I wanted to find out what was the exact input to the test. Luckily for me, the designer for this assignment left out a point to exploit.

The automated grader would first take the uploaded files and compile them with the on-board test suite, and then run the tests. After running the tests, it would dump the output from valgrind to give the student some indication on memory usage and leakage. The problem here is that valgrind doesn't only print memory usage and leakage. It also prints anything in stdout and stderr. I also noticed that the Makefile build was echoed on screen. I not only had the name of the file for private testing, but also the means for printing anything out. Which means:

#include <fstream>
#include <sstream>
#include <iostream>

using namespace std;

void someFunctionThatOnlyRunsOnce() {
	ifstream is("testgrid.cpp");
	if (is.is_open()) {
		stringstream ss;
		ss << is.rdbuf();
		cerr << ss.str();
	}
}

After compiling to make sure nothing breaks, I uploaded it for the grader to run and got my results back. I found that the bug was not in my code, but in the form of a hidden requirement. The goal of this assignment was to work with a grid in the form of 2D linked lists. The hidden requirement was that the tests expected the program to take row and column input that were larger than the actual grid. According to the test, the actual grid should be infinite, with the rows and columns cycling. To adhere to these new requirements, I would simply take the inputs to the function and (mod <row-size|col-size>). This was not specified in the assignment details.

And so I went and made the changes. All was well.

Aftermath

A few hours after I finished working on this programming assignment, there was an announcement about the homework that talked about this hidden requirement. The things I do for marks....

Tags: assignment c++ writeup