Permalink
Browse files

Throws exception when offset was not an integer

  • Loading branch information...
1 parent 001fb2a commit 114a31f35357a0b67a1951b94e92cc6a9a54a8a2 @koichik committed Jun 20, 2011
Showing with 43 additions and 9 deletions.
  1. +30 −9 src/node_file.cc
  2. +13 −0 test/disabled/test-fs-largefile.js
View
@@ -81,6 +81,12 @@ static inline bool SetCloseOnExec(int fd) {
#endif
}
+#ifdef _LARGEFILE_SOURCE
+static inline int IsInt64(double x) {
+ return x == static_cast<double>(static_cast<int64_t>(x));
+}
+#endif
+
static int After(eio_req *req) {
HandleScope scope;
@@ -451,9 +457,17 @@ static Handle<Value> Rename(const Arguments& args) {
}
#ifndef _LARGEFILE_SOURCE
-#define GET_TRUNCATE_LEN(a) (a)->UInt32Value();
+#define ASSERT_TRUNCATE_LENGTH(a) \
+ if (!(a)->IsUndefined() && !(a)->IsNull() && !(a)->IsUInt32()) { \
+ return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
+ }
+#define GET_TRUNCATE_LENGTH(a) ((a)->UInt32Value())
#else
-#define GET_TRUNCATE_LEN(a) (a)->IntegerValue();
+#define ASSERT_TRUNCATE_LENGTH(a) \
+ if (!(a)->IsUndefined() && !(a)->IsNull() && !IsInt64((a)->NumberValue())) { \
+ return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
+ }
+#define GET_TRUNCATE_LENGTH(a) ((a)->IntegerValue())
#endif
static Handle<Value> Truncate(const Arguments& args) {
@@ -464,7 +478,9 @@ static Handle<Value> Truncate(const Arguments& args) {
}
int fd = args[0]->Int32Value();
- off_t len = GET_TRUNCATE_LEN(args[1]);
+
+ ASSERT_TRUNCATE_LENGTH(args[1]);
+ off_t len = GET_TRUNCATE_LENGTH(args[1]);
if (args[2]->IsFunction()) {
ASYNC_CALL(ftruncate, args[2], fd, len)
@@ -667,13 +683,17 @@ static Handle<Value> Open(const Arguments& args) {
}
#ifndef _LARGEFILE_SOURCE
-#define GET_OFFSET(a) (a)->IsInt32() ? (a)->IntegerValue() : -1;
+#define ASSERT_OFFSET(a) \
+ if (!(a)->IsUndefined() && !(a)->IsNull() && !(a)->IsInt32()) { \
+ return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
+ }
+#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->Int32Value() : -1)
#else
-static inline int IsInt64(double x) {
- return x == static_cast<double>(static_cast<int64_t>(x));
-}
-#define GET_OFFSET(a) \
- (a)->IsNumber() && IsInt64((a)->NumberValue()) ? (a)->IntegerValue() : -1;
+#define ASSERT_OFFSET(a) \
+ if (!(a)->IsUndefined() && !(a)->IsNull() && !IsInt64((a)->NumberValue())) { \
+ return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
+ }
+#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)
#endif
// bytesWritten = write(fd, data, position, enc, callback)
@@ -715,6 +735,7 @@ static Handle<Value> Write(const Arguments& args) {
String::New("Length is extends beyond buffer")));
}
+ ASSERT_OFFSET(args[4]);
off_t pos = GET_OFFSET(args[4]);
char * buf = (char*)buffer_data + off;
@@ -38,3 +38,16 @@ assert.equal(readBuf.toString(), message);
fs.readSync(fd, readBuf, 0, 1, 0);
assert.equal(readBuf[0], 0);
+var exceptionRaised = false;
+try {
+ fs.writeSync(fd, writeBuf, 0, writeBuf.length, 42.000001);
+} catch (err) {
+ console.log(err);
+ exceptionRaised = true;
+ assert.equal(err.message, 'Not an integer');
+}
+
+process.on('exit', function() {
+ assert.ok(exceptionRaised);
+});
+

0 comments on commit 114a31f

Please sign in to comment.