natevw / fatfs

Standalone FAT16/FAT32 filesystem implementation in JavaScript
47 stars 13 forks source link

Execution continues even after EBADF #34

Closed zwhitchcox closed 2 years ago

zwhitchcox commented 2 years ago

For fs.read and fs.write, execution continues even after S.err.EBADF is called:

    fs.write = function write(fd, buf, off, len, pos, cb, _n_) {
        cb = GROUP(cb, function () {
            var _fd = fileDescriptors[fd];
            if (!_fd || !_fd.flags.write) _.delayedCall(cb, S.err.EBADF());
            // execution continues

Whereas the function should return once it is determined to have a bad fd:

    fs.write = function write(fd, buf, off, len, pos, cb, _n_) {
        cb = GROUP(cb, function () {
            var _fd = fileDescriptors[fd];
            if (!_fd || !_fd.flags.write) return _.delayedCall(cb, S.err.EBADF());

In general, all of the if (!_fd... calls could just return, eliminating the need for the else statements in the other functions as well, so instead of:

            if (!_fd || !_fd.flags.read) _.delayedCall(cb, S.err.EBADF());
            else {

Just:

            if (!_fd || !_fd.flags.read) return _.delayedCall(cb, S.err.EBADF());

which makes for cleaner "less-nested" code.

natevw commented 2 years ago

Thanks for this detailed report, and sorry for the delay responding! This should now be fixed in fatfs@0.10.8.