caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Thomas Braibant <thomas.braibant@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] Hashing failure
Date: Thu, 20 Dec 2012 21:18:32 +0100	[thread overview]
Message-ID: <CAHR=Vkx3hntWcQYV8tptKnrRyKHNBdfA-6KxjfxWEp=j4Q-saQ@mail.gmail.com> (raw)
In-Reply-To: <CAHR=VkzBhR5FMsWO-_BVzYV4yhSARP8rg7G3b=jezU6OOOg0jQ@mail.gmail.com>

Hi list,

I have a piece of code that looks like this.

let hkey = H.hash d in (* a custom hash function *)
let index = hkey mod (Array.length t.table) in
let bucket = t.table.(index) in (* 1 *)

the line marked with 1 was generating an index out of bounds
exception. It appeared that it was because index was negative, a
problem  I knew of when using mod ... So I added an abs around index

let hkey = H.hash d in
let index =  abs (hkey mod (Array.length t.table)) in
let bucket = t.table.(index) in (* 1 *)

But it yields the same exception...

This seemed completely unbelievable, till I realized that
- x mod y may return a negative value if x is less than 0
- abs x may return a negative value if x is min_int

So I tried to reproduce this behavior with the following code:

let t = Array.create 17 0;;
let hkey = min_int in
let index = (abs hkey) mod (Array.length t) in
t.(index);;

which yields an out of bound. Bingo. It turns out that some value of
the length of t will result in an error, and some will not (e.g., 16) ... My
question is : is there a safe way to bullet-proof my code against
these pathological cases, without too much hinderance (this is some
code whose efficiency is critical...)


Thomas

       reply	other threads:[~2012-12-20 20:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAHR=VkzBhR5FMsWO-_BVzYV4yhSARP8rg7G3b=jezU6OOOg0jQ@mail.gmail.com>
2012-12-20 20:18 ` Thomas Braibant [this message]
2012-12-20 20:32   ` [Caml-list] " Thomas Braibant
2012-12-20 21:01     ` Martin Jambon
2012-12-21  7:58   ` [Caml-list] " Jacques Garrigue
2012-12-21 10:02   ` Xavier Leroy
2012-12-21 19:03     ` Martin Jambon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHR=Vkx3hntWcQYV8tptKnrRyKHNBdfA-6KxjfxWEp=j4Q-saQ@mail.gmail.com' \
    --to=thomas.braibant@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).