natevw / fatfs

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

Out of range index on Buffer.copy #28

Closed xarasbir closed 5 years ago

xarasbir commented 5 years ago

I'm getting out of range index error when executing writeFile and mkdir (and probably other fs commands I haven't tried as well)

My code is pretty straight forward. What I intend to do is just to create a directory and write a file under it. Here's the snippet

var vol = imgVol.createDriverSync("freedos722.img");
var fs = fatfs.createFileSystem(vol);
fs.mkdir("newdir", function(){
   console.log("created");
})

freedos722.img is a bootable freedos image file. The exact img file I'm using can be found here : https://drive.google.com/open?id=1AqUOCAv3kYgSLDAcWi6PnRhi1RHHsJ8U

natevw commented 5 years ago

Appreciate the report, hoping we can track this down soon.

To troubleshoot, I'm starting with the following code as "issue_28.js" right in my working copy of the repo:

var fatfs = require("./"),
    imgVol = require("./img_volume.js");

var vol = imgVol.createDriverSync("freedos722.img");
var fs = fatfs.createFileSystem(vol);
fs.mkdir("newdir", function(){
   console.log("created");
});

With node v10.12.0, I get the following error whether the freedos722.img is in path or not (!):

$ node issue_28.js
internal/buffer.js:51
    throw new ERR_BUFFER_OUT_OF_BOUNDS();
    ^

RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: Attempt to write outside buffer bounds
    at boundsError (internal/buffer.js:51:11)
    at Buffer.readUInt32BE (internal/buffer.js:196:5)
    at Object.<anonymous> (/Users/natevw/Desktop/Clients/•archive•/Technical_Machine/fatfs/node_modules/struct-fu/lib.js:3:19)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)

This looks like a different error though, so I may file a separate issue unless it ends up being related. [UPDATE: yeah, this was #27, fixed by updating my own repo https://github.com/natevw/struct-fu/issues/18]

(Thanks for posting the image and sample code, btw! Definitely saves me some time since I'm kind of re-learning how to use my own library now :-)

natevw commented 5 years ago

Ah, one idea that comes to mind is that maybe this image has a partition map on it? There's a comment in the example driver:

// NOTE: this assumes image at `path` has no partition table.
//       If it did, you'd need to translate positions, natch…

I also wrote https://github.com/natevw/parsetition that would deal with that. I know we had a more "automatic" working demo for the original Tessel. I'll try track down the library or sample code that tied fatfs/parsetition together (it may have been tied to the sdcard driver…).

natevw commented 5 years ago

Hmm, hdiutil on my Mac doesn't seem to think there's partitions, so maybe it's something else:

$ hdiutil imageinfo /Users/natevw/Downloads/freedos722.img 
Backing Store Information:
    URL: file:///Users/natevw/Downloads/freedos722.img
    Name: freedos722.img
    Class Name: CBSDBackingStore
Class Name: CRawDiskImage
Checksum Type: none
Size Information:
    Total Bytes: 737280
    Compressed Ratio: 1
    Sector Count: 1440
    Total Non-Empty Bytes: 737280
    Compressed Bytes: 737280
    Total Empty Bytes: 0
Format: RAW*
Format Description: raw read/write
Checksum Value: 
Properties:
    Encrypted: false
    Kernel Compatible: true
    Checksummed: false
    Software License Agreement: false
    Partitioned: false
    Compressed: no
Segments:
    0: /Users/natevw/Downloads/freedos722.img
partitions:
    partition-scheme: none
    block-size: 512
    appendable: false
    partitions:
        0:
            partition-name: whole disk
            partition-start: 0
            partition-synthesized: true
            partition-length: 1440
            partition-hint: DOS_FAT_12
            partition-filesystems:
                FAT12: FREEDOS
    burnable: false
natevw commented 5 years ago

I found the bug, if you are able to run the master branch you can confirm. Otherwise I am hoping to fix up some buffer deprecation warnings (#29) before pushing as a new release.

natevw commented 5 years ago

Fixed pushed as fatfs@0.10.7 — thanks again for the report and please let me know how it goes!

xarasbir commented 5 years ago

Just updated to version 0.10.7 and the error is gone. Thank you @natevw. Appreciate the quick action.