You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fs.ftruncate silently accepts negative offsets rather than failing with ERR_INVALID #35632
Maybe removing len = MathMax(0, len); would fix the issue, or by explicitly handling the negative value case before calling the internal method. Had experienced this issue, a week back ! I solved this by explicitly checking the len before passing it down to the ftruncateSync()
- len = MathMax(0, len);+ if (len < 0) {+ throw new ERR_FS_FILE_TOO_SMALL(len);+ } else if (len > kIoMaxLength) {+ throw new ERR_FS_FILE_TOO_LARGE(len);+ }
where, ERR_FS_FILE_TOO_LARGE already exists and we make a new error code ERR_FS_FILE_TOO_SMALL for negative sizes and this is all about kIoMaxLength:
Node 8.xx.xx throws EINVAL properly for negative offset. Maybe there is no need to handle explicitly. Even len = MathMax(0, len) is not required. The internal system call itself terminates with EINVAL when it sees negative offset. So no need to handle anything explicitly. If you check the source code of 8.xx.xx there is no explicit validation being done.
Activity
Narasimha1997 commentedon Oct 14, 2020
Maybe removing
len = MathMax(0, len);would fix the issue, or by explicitly handling the negative value case before calling the internal method. Had experienced this issue, a week back ! I solved this by explicitly checking thelenbefore passing it down to theftruncateSync()RaisinTen commentedon Oct 14, 2020
What about adding this sort of a change?
where,
ERR_FS_FILE_TOO_LARGEalready exists and we make a new error codeERR_FS_FILE_TOO_SMALLfor negative sizes and this is all aboutkIoMaxLength:node/lib/fs.js
Lines 27 to 29 in 999e7d7
This would take care of both the upper and the lower limits for the allowed size in
ftruncate.sbc100 commentedon Oct 14, 2020
I'm not familiar with node code but isn't there already the simple equivalent of
EINVAL? Is it a good idea to add more error codes?Narasimha1997 commentedon Oct 14, 2020
Node
8.xx.xxthrowsEINVALproperly for negative offset. Maybe there is no need to handle explicitly. Evenlen = MathMax(0, len)is not required. The internal system call itself terminates withEINVALwhen it sees negative offset. So no need to handle anything explicitly. If you check the source code of8.xx.xxthere is no explicit validation being done.jasnell commentedon Feb 22, 2021
PR: #37483
fs: fixup negative length in fs.truncate
fs: fixup negative length in fs.truncate
fs: fixup negative length in fs.truncate
fs: fixup negative length in fs.truncate