English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Causes for segfaults
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-11-15 (18:17)
From: John Whitington <john@c...>
Subject: Re: [Caml-list] Causes for segfaults

On 15/11/2010 17:58, Jamie Brandon wrote:
> I do feel that this would have been much easier to fix if ocaml raised
> more informative errros. The change to Array.make and String.make to
> raise exceptions instead of segfaulting is welcome. Is there a way to
> cause stack overflows to raise exceptions in native compiled ocaml as
> well?

Excessive recursion of a function does raise an exception, on most 
platforms, doesn't it?

However, this doesn't seem to be the case with excessive nested 
exceptions. I got caught by this the other day:

let rec lex_stream i acc =
   try lex_stream i (lex_next i::acc) with LexingEnd -> rev acc

(I had been replacing option types with exceptions in some places for 
speed and wasn't concentrating...)

This segfaults on OS X Intel with OCaml 3.12.0 on large inputs.

The workaround is to add a null constructor for the type e.g LexNone. In 
the absence of one, I think you have to do:

let rec lex_stream i acc =
   match try Some (lex_stream i) with LexingEnd -> None with
   | None -> rev acc
   | Some s -> lex_stream i (a::acc)

Hopefully that option type doesn't slow things down too much.


John Whitington
Director, Coherent Graphics Ltd