Skip to content

Commit

Permalink
Runtime: fix some possible integer overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Jan 28, 2025
1 parent ddc1da6 commit c44779c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
15 changes: 8 additions & 7 deletions runtime/js/fs_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function fs_node_supported() {
//Provides: MlNodeDevice
//Requires: MlNodeFd, caml_raise_sys_error, caml_raise_with_args
//Requires: make_unix_err_args, caml_named_value, caml_string_of_jsstring
//Requires: caml_int64_of_float
function MlNodeDevice(root) {
this.fs = require("node:fs");
this.root = root;
Expand Down Expand Up @@ -182,18 +183,18 @@ if (globalThis.process?.platform === "win32") {
};
}

MlNodeDevice.prototype.stat = function (name, raise_unix) {
MlNodeDevice.prototype.stat = function (name, large, raise_unix) {
try {
var js_stats = this.fs.statSync(this.nm(name));
return this.stats_from_js(js_stats);
return this.stats_from_js(js_stats, large);
} catch (err) {
this.raise_nodejs_error(err, raise_unix);
}
};
MlNodeDevice.prototype.lstat = function (name, raise_unix) {
MlNodeDevice.prototype.lstat = function (name, large, raise_unix) {
try {
var js_stats = this.fs.lstatSync(this.nm(name));
return this.stats_from_js(js_stats);
return this.stats_from_js(js_stats, large);
} catch (err) {
this.raise_nodejs_error(err, raise_unix);
}
Expand Down Expand Up @@ -234,7 +235,7 @@ MlNodeDevice.prototype.raise_nodejs_error = function (err, raise_unix) {
caml_raise_sys_error(err.toString());
}
};
MlNodeDevice.prototype.stats_from_js = function (js_stats) {
MlNodeDevice.prototype.stats_from_js = function (js_stats, large) {
/* ===Unix.file_kind===
* type file_kind =
* S_REG (** Regular file *)
Expand Down Expand Up @@ -280,14 +281,14 @@ MlNodeDevice.prototype.stats_from_js = function (js_stats) {
return BLOCK(
0,
js_stats.dev,
js_stats.ino,
js_stats.ino | 0,
file_kind,
js_stats.mode,
js_stats.nlink,
js_stats.uid,
js_stats.gid,
js_stats.rdev,
js_stats.size,
large ? caml_int64_of_float(js_stats.size) : js_stats.size | 0,
js_stats.atimeMs / 1000,
js_stats.mtimeMs / 1000,
js_stats.ctimeMs / 1000,
Expand Down
8 changes: 4 additions & 4 deletions runtime/js/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ function caml_ml_close_channel(chanid) {
//Requires: caml_ml_channel_get
function caml_ml_channel_size(chanid) {
var chan = caml_ml_channel_get(chanid);
return chan.file.length();
return chan.file.length() | 0;
}

//Provides: caml_ml_channel_size_64
Expand Down Expand Up @@ -519,13 +519,13 @@ function caml_ml_seek_in_64(chanid, pos) {
//Requires: caml_ml_channel_get
function caml_pos_in(chanid) {
var chan = caml_ml_channel_get(chanid);
return (chan.offset - (chan.buffer_max - chan.buffer_curr)) | 0;
return chan.offset - (chan.buffer_max - chan.buffer_curr);
}

//Provides: caml_ml_pos_in
//Requires: caml_pos_in
function caml_ml_pos_in(chanid) {
return caml_pos_in(chanid);
return caml_pos_in(chanid) | 0;
}

//Provides: caml_ml_pos_in_64
Expand Down Expand Up @@ -697,7 +697,7 @@ function caml_pos_out(chanid) {
//Provides: caml_ml_pos_out
//Requires: caml_pos_out
function caml_ml_pos_out(chanid) {
return caml_pos_out(chanid);
return caml_pos_out(chanid) | 0;
}

//Provides: caml_ml_pos_out_64
Expand Down
24 changes: 14 additions & 10 deletions runtime/js/unix.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,18 @@ function caml_unix_stat(name) {
if (!root.device.stat) {
caml_failwith("caml_unix_stat: not implemented");
}
return root.device.stat(root.rest, /* raise Unix_error */ true);
return root.device.stat(root.rest, /* large */ false, /* raise Unix_error */ true);
}

//Provides: caml_unix_stat_64
//Requires: caml_unix_stat, caml_int64_of_int32
//Requires: resolve_fs_device, caml_failwith
//Alias: unix_stat_64
function caml_unix_stat_64(name) {
var r = caml_unix_stat(name);
r[9] = caml_int64_of_int32(r[9]);
return r;
var root = resolve_fs_device(name);
if (!root.device.stat) {
caml_failwith("caml_unix_stat: not implemented");
}
return root.device.stat(root.rest, /* large */ true, /* raise Unix_error */ true);
}

//Provides: caml_unix_lstat
Expand All @@ -224,16 +226,18 @@ function caml_unix_lstat(name) {
if (!root.device.lstat) {
caml_failwith("caml_unix_lstat: not implemented");
}
return root.device.lstat(root.rest, /* raise Unix_error */ true);
return root.device.lstat(root.rest, /* large */ false, /* raise Unix_error */ true);
}

//Provides: caml_unix_lstat_64
//Requires: caml_unix_lstat, caml_int64_of_int32
//Requires: resolve_fs_device, caml_failwith
//Alias: unix_lstat_64
function caml_unix_lstat_64(name) {
var r = caml_unix_lstat(name);
r[9] = caml_int64_of_int32(r[9]);
return r;
var root = resolve_fs_device(name);
if (!root.device.lstat) {
caml_failwith("caml_unix_lstat: not implemented");
}
return root.device.lstat(root.rest, /* large */ true, /* raise Unix_error */ true);
}

//Provides: caml_unix_mkdir
Expand Down

0 comments on commit c44779c

Please sign in to comment.