caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Bug in caml_string_get16?
@ 2013-11-04 22:54 Nicolas Trangez
  2013-11-05  3:54 ` Ivan Gotovchits
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas Trangez @ 2013-11-04 22:54 UTC (permalink / raw)
  To: caml-list

All,

I was playing around with *ocplib-endian* a couple of days ago (to
access the `%caml_string_getN` primops), and found something strange
going on (which I think might be a bug).

What would you expect to be the output of this code?

```ocaml
external get_16 : string -> int -> int = "%caml_string_get16"

let main () =
  let () = try
    let i = get_16 "1" 0 in
    Printf.printf "Uh-oh 1: %d\n" i
  with exn ->
    Printf.printf "Got %s :-)\n" (Printexc.to_string exn)
  in

  try
    let i = get_16 "" 0 in
    Printf.printf "Uh-oh 2: %d\n" i
  with exn ->
    Printf.printf "Got %s :-)\n" (Printexc.to_string exn)
;;

main ()
```

Here's what it does on my system:

```
$ ocamlopt -version
4.01.0
$ ocamlbuild -use-ocamlfind parse.native
Finished, 4 targets (0 cached) in 00:00:00.
$ ./parse.native 
Got Invalid_argument("index out of bounds") :-)
Uh-oh 2: 0
```

I think there's something fishy going on here...

I didn't test, but I guess other related functions (32/64 bit & unsafe
variants) could be affected as well.

Nicolas


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Bug in caml_string_get16?
  2013-11-04 22:54 [Caml-list] Bug in caml_string_get16? Nicolas Trangez
@ 2013-11-05  3:54 ` Ivan Gotovchits
  2013-11-05  9:53   ` Nicolas Trangez
  0 siblings, 1 reply; 4+ messages in thread
From: Ivan Gotovchits @ 2013-11-05  3:54 UTC (permalink / raw)
  To: Nicolas Trangez; +Cc: caml-list

Nicolas Trangez <nicolas@incubaid.com> writes:

> What would you expect to be the output of this code?

It should fail :)

In a string literal any symbol that you can type with your keyboard is
stored in a special encoding called ASCII. With "1" you have just
created a string containig a single byte with a value 49. So, first off
all, your string is too short, because it has length of 1, when int16
obviously needs two bytes of storage space.  If you really interested,
binary 1 will be represented in a memory of a little endian computer
like this: "\x01\x00". The special escape symbol "\" allows us to type
untypable. The the code

    open EndianString
    let d = LittleEndian.get_int16 "\x01\x00" 0

will yield:

    val d : int = 1

By the way, if we try your string (with a more correct invocation):

    open EndianString
    let d = LittleEndian.get_int8 "1" 0

we will got:

    val d : int = 49

Futher reading:

1. man ascii
2. wikipedia article on endianess 


Good luck!

-- 
         (__) 
         (oo) 
   /------\/ 
  / |    ||   
 *  /\---/\ 
    ~~   ~~   
...."Have you mooed today?"...

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Bug in caml_string_get16?
  2013-11-05  3:54 ` Ivan Gotovchits
@ 2013-11-05  9:53   ` Nicolas Trangez
  2013-11-05 10:49     ` Gabriel Scherer
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas Trangez @ 2013-11-05  9:53 UTC (permalink / raw)
  To: Ivan Gotovchits; +Cc: caml-list

On Tue, 2013-11-05 at 07:54 +0400, Ivan Gotovchits wrote:
> Nicolas Trangez <nicolas@incubaid.com> writes:
> 
> > What would you expect to be the output of this code?
> 
> It should fail :)
> 
> In a string literal any symbol that you can type with your keyboard is
> stored in a special encoding called ASCII. With "1" you have just
> created a string containig a single byte with a value 49. So, first off
> all, your string is too short, because it has length of 1, when int16
> obviously needs two bytes of storage space.  If you really interested,
> binary 1 will be represented in a memory of a little endian computer
> like this: "\x01\x00". The special escape symbol "\" allows us to type
> untypable. The the code
> 
>     open EndianString
>     let d = LittleEndian.get_int16 "\x01\x00" 0
> 
> will yield:
> 
>     val d : int = 1
> 
> By the way, if we try your string (with a more correct invocation):
> 
>     open EndianString
>     let d = LittleEndian.get_int8 "1" 0
> 
> we will got:
> 
>     val d : int = 49
> 
> Futher reading:
> 
> 1. man ascii
> 2. wikipedia article on endianess 

Oh, don't be mistaken. The question was rhetorical, in order to report a
bug (or get told it's not a bug at all) & provide a test-case :-)
I'm well aware of all of the above ;-)

Thanks!

Nicolas


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Bug in caml_string_get16?
  2013-11-05  9:53   ` Nicolas Trangez
@ 2013-11-05 10:49     ` Gabriel Scherer
  0 siblings, 0 replies; 4+ messages in thread
From: Gabriel Scherer @ 2013-11-05 10:49 UTC (permalink / raw)
  To: Nicolas Trangez; +Cc: Ivan Gotovchits, caml users

Despite the somewhat unclear reporting (I didn't read the first mail
carefully enough to understand the problem), this is in fact a
report for a very serious bug. I have a fix in the works that should
end upstream quickly.

        OCaml version 4.01.0

# external set64 : string -> int -> int -> unit = "caml_string_set64";;
external set64 : string -> int -> int -> unit = "caml_string_set64"
# set64 "0" 1 42;;
zsh: segmentation fault

On Tue, Nov 5, 2013 at 10:53 AM, Nicolas Trangez <nicolas@incubaid.com> wrote:
> On Tue, 2013-11-05 at 07:54 +0400, Ivan Gotovchits wrote:
>> Nicolas Trangez <nicolas@incubaid.com> writes:
>>
>> > What would you expect to be the output of this code?
>>
>> It should fail :)
>>
>> In a string literal any symbol that you can type with your keyboard is
>> stored in a special encoding called ASCII. With "1" you have just
>> created a string containig a single byte with a value 49. So, first off
>> all, your string is too short, because it has length of 1, when int16
>> obviously needs two bytes of storage space.  If you really interested,
>> binary 1 will be represented in a memory of a little endian computer
>> like this: "\x01\x00". The special escape symbol "\" allows us to type
>> untypable. The the code
>>
>>     open EndianString
>>     let d = LittleEndian.get_int16 "\x01\x00" 0
>>
>> will yield:
>>
>>     val d : int = 1
>>
>> By the way, if we try your string (with a more correct invocation):
>>
>>     open EndianString
>>     let d = LittleEndian.get_int8 "1" 0
>>
>> we will got:
>>
>>     val d : int = 49
>>
>> Futher reading:
>>
>> 1. man ascii
>> 2. wikipedia article on endianess
>
> Oh, don't be mistaken. The question was rhetorical, in order to report a
> bug (or get told it's not a bug at all) & provide a test-case :-)
> I'm well aware of all of the above ;-)
>
> Thanks!
>
> Nicolas
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-11-05 10:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-04 22:54 [Caml-list] Bug in caml_string_get16? Nicolas Trangez
2013-11-05  3:54 ` Ivan Gotovchits
2013-11-05  9:53   ` Nicolas Trangez
2013-11-05 10:49     ` Gabriel Scherer

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).