Invalid argument exception when trying to seek FilePosition to LONG.MAX_VALUE

Karan Mehta k.mehta at salesforce.com
Tue Jun 4 20:07:04 UTC 2019


> The code uses lseek64 and off64_t so I don't think this is the issue. You
can verify this by running with strace and you should see output similar to
what I had in the first mail.

I ran strace on a 64 bit Linux box but I see calls to only lseek instead of
lseek64. Am I missing something here? What does it show up on mac?

> The IOException is correct, it's just the "Illegal argument" from the
syscall is confusing.

Should the IOException be swallowed here according to the FileChannel
interface contract and the resulting operation be no-op for the caller
instead? Does this mean that any file size longer than INT.MAX_VALUE can
potentially cause a problem here, either when trying to read from it or
seeking it?

On Tue, Jun 4, 2019 at 11:47 AM Alan Bateman <Alan.Bateman at oracle.com>
wrote:

> On 04/06/2019 19:31, Karan Mehta wrote:
>
> Thanks for your quick response. Appreciate it.
>
> The link for man page seems a bit contradictory. At the top, it says,
> "lseek() allows the file offset to be set beyond the end of the file (but
> this does not change the size of the file)." but it is followed by the text
> you mentioned.
> The function declaration is `off_t lseek(int fd, off_t offset, int
> whence);` where the offset is defined with off_t type. I was trying to
> reason its size based on stackoverflow here
> <https://stackoverflow.com/questions/9073667/where-to-find-the-complete-definition-of-off-t-type>.
> The link
> <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html#tag_13_67> suggests
> that they would be signed integers and hence this is a potential overflow
> and might result in negative value, causing it to be a reason for
> exception. Should the JNI call be using this
> <https://stackoverflow.com/a/14351239/4428999> instead since the Java
> interface offers the option to pass long variable for the offset.
>
> The code uses lseek64 and off64_t so I don't think this is the issue. You
> can verify this by running with strace and you should see output similar to
> what I had in the first mail.
>
> What should be the correct fix here? Of course, if the underlying OS is 32
> bit, even the SO link above won't help but it would make it work on almost
> all modern hardware.
>
> The IOException is correct, it's just the "Illegal argument" from the
> syscall is confusing.
>
> -Alan
>


-- 
Karan Mehta

<http://smart.salesforce.com/sig/k.mehta//us_mb/default/link.html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/nio-dev/attachments/20190604/67e1b1a7/attachment.html>


More information about the nio-dev mailing list