Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integer overflows in the standard library #3420

Closed
vicuna opened this issue Jul 9, 2002 · 2 comments
Closed

integer overflows in the standard library #3420

vicuna opened this issue Jul 9, 2002 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Jul 9, 2002

Original bug ID: 1229
Reporter: administrator
Status: closed
Resolution: fixed
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Full_Name: Andy Chou
Version: 3.04
OS: Linux
Submission from: acc.stanford.edu (128.12.185.77)

Hi,

The standard library has many places where insufficient checking is done for
integer overflow. Here are a few examples that cause seg faults (certainly
there are more places where this happens):

(* the input/output functions in Pervasives *)
let _ =
let f = open_out "f" in
output f "" 0x3fffffff 1

(* String *)
let _ = String.sub "" 0x3fffffff 1
let _ = String.fill "" 0x3fffffff 1 'a'

(* Buffer *)
let _ =
let b = Buffer.create 10 in
Buffer.add_substring b "" 0x3fffffff 1

The problem is basically code like the following:

if ofs < 0 || len < 0 || ofs + len > length s
then invalid_arg "String.sub"

where ofs + len might overflow. A better check might look like:

if ofs < 0 || len < 0 || ofs + len < 0 || ofs + len > length s

Better yet, if unsigned comparison was made available from Ocaml using, say,
operator ">u", then you could write:

if ofs < 0 || len < 0 || ofs + len >u length s

By the way, I've really enjoyed learning Ocaml. Thanks for all the hard work
making this language come to life in a powerful way.

-Andy

@vicuna
Copy link
Author

vicuna commented Jul 11, 2002

Comment author: administrator

The standard library has many places where insufficient checking is done for
integer overflow.
The problem is basically code like the following:

if ofs < 0 || len < 0 || ofs + len > length s
then invalid_arg "String.sub"

where ofs + len might overflow.

You're entirely right, and this bug has been around for ages. (Blush.)

A better check might look like:

if ofs < 0 || len < 0 || ofs + len < 0 || ofs + len > length s

Better yet, if unsigned comparison was made available from Ocaml using, say,
operator ">u", then you could write:

if ofs < 0 || len < 0 || ofs + len >u length s

I believe that

if ofs < 0 || len < 0 || ofs > length s - len

works as well, since subtraction between two non-negative integers cannot
overflow. I'd better double-check my proof of this fact, though!

Thanks for a well-spotted bug report.

  • Xavier Leroy

@vicuna
Copy link
Author

vicuna commented Jul 12, 2002

Comment author: administrator

Fixed 2002-07-12 by XL

@vicuna vicuna closed this as completed Jul 12, 2002
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant