To: Benedikt-Grundmann at web.de Cc: lablgtk at kaba.or.jp Subject: Re: BUG report In-Reply-To: <200402130009.50474.Benedikt-Grundmann at web.de> References: <200402130009.50474.Benedikt-Grundmann at web.de> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20040213173555E.garrigue at kurims.kyoto-u.ac.jp> Date: Fri, 13 Feb 2004 17:35:55 +0900 From: Jacques Garrigue Lines: 75 From: Benedikt Grundmann > I discovered a bug in the Text widget binding concerning > selection handling: > > CAMLprim value ml_gtk_text_buffer_get_selection_bounds(value tb) [...] > > This code obviously implements a primitive of type text_buffer -> > (iter * iter) option. But looking in gtkText.ml and > gText.mli/gText.ml I was surprised to find out that it's declared as > text_buffer -> iter * iter therefore resulting in an segmentation > fault whenever one uses calls buffer#selection_bounds!! Right. > I fixed the type declarations and changed the implementation in > gText.ml to this: > > method selection_bounds = > Gaux.may_map ~f:(fun (start, stop) -> > (new iter start, new iter stop)) (Buffer.get_selection_bounds obj) > > Surprisingly I still get a sigsev but only when I call it when NO > selection is active! This code seems completely broken: * the cases for e are wrong: it is true when the selection is _not_ null * the iters are written directly to res rather than to couple The combination of these two bugs mean that either * the selection is non-empty, but you get None * the selection is empty, and you get a segv Looking at the specification, there is no strong reason to return an option here: the iterators are always correctly set, and you can just do [i1#equal i2] to check whether the selection is empty. So you can use the following patch. Jacques Index: ml_gtktext.c =================================================================== RCS file: /staff2/garrigue/repos/lablgtk/src/ml_gtktext.c,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- ml_gtktext.c 8 Jan 2004 00:54:29 -0000 1.16 +++ ml_gtktext.c 13 Feb 2004 08:25:49 -0000 1.17 @@ -433,19 +433,12 @@ CAMLprim value ml_gtk_text_buffer_get_selection_bounds(value tb) { CAMLparam1(tb); - CAMLlocal2(res,couple); + CAMLlocal1(res); GtkTextIter res1,res2; - int e; - e=gtk_text_buffer_get_selection_bounds(GtkTextBuffer_val(tb), &res1, &res2); - - if (e) res = Val_unit; - else { - couple = alloc_tuple(2); - Store_field(res,0,Val_GtkTextIter(&res1)); - Store_field(res,1,Val_GtkTextIter(&res2)); - res = ml_some(couple); - }; - + gtk_text_buffer_get_selection_bounds(GtkTextBuffer_val(tb), &res1, &res2); + res = alloc_tuple(2); + Store_field(res,0,Val_GtkTextIter(&res1)); + Store_field(res,1,Val_GtkTextIter(&res2)); CAMLreturn(res); }