Computer science engineering student and software developer at Gulliver
f0lg0 / chip-8 Goto Github PK
View Code? Open in Web Editor NEWA simple emulator for the CHIP-8 interpreted programming language.
License: MIT License
A simple emulator for the CHIP-8 interpreted programming language.
License: MIT License
Computer science engineering student and software developer at Gulliver
At line 20 of main.c, check_rom() is called. Soon after, load_rom() is called at line 25.
This causes several problems:
check_rom() fopen()s a file and doesn't close it before returning, causing the open FILE to leak. Additionally, it unnecessarily makes any further attempt at opening the file implementation-defined behavior, although I don't know of a C implementation where opening the same file multiple times doesn't just work.
The load_rom() call also fopen()s the same file, without any checking of return values. A non-existing file is not the only cause for an error return of fopen().
Even if the above issues are fixed, there is an inherent TOCTTOU error in doing this. There is no guarantee that the file will still exist by the time load_rom() is called.
The fix is to get rid of the check_rom() function and check the return value of fopen() in load_rom().
As suggested by a oh5nxo on Reddit, the function load_rom()
could be improved in the following way:
void load_rom(char* filename) {
FILE* fp = fopen(filename, "rb");
// read program size into memory
fread(memory + 0x200, 1, sizeof(memory) - 0x200, fp);
fclose(fp);
}
From a quick first test, it seems good. Waiting to update to give it a better look.
`
// FX55: Stores V0 through Vx (Vx included) in memory starting
// at addr I.
case 0x0055:
debug_print("[OK] 0x%X: FX55\n", op);
for (int i = 0; i <= x; i++) {
V[i] = memory[I + i];
}
pc += 2;
break;
`
shouldn't this segment of code be like this:
`// FX55: Stores V0 through Vx (Vx included) in memory starting
// at addr I.
case 0x0055:
debug_print("[OK] 0x%X: FX55\n", op);
for (int i = 0; i <= x; i++) {
memory[I + i] = V[i];
}
pc += 2;
break;`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.