ocaml / ocaml

The core OCaml system: compilers, runtime system, base libraries
https://ocaml.org
Other
5.19k stars 1.06k forks source link

Re: getsockopt: incorrect return values #3491

Closed vicuna closed 21 years ago

vicuna commented 21 years ago

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

Bug description

Sorry -- forgot to say that I'm running Ocaml 3.04 on FreeBSD v4.6.

Thanks, Steve

On Wed, 2002-07-31 at 13:30, Steven Bishop wrote:

Hi,

The following program terminates producing an incorrect result of !SO_KEEPALIVE but will terminate correctly if the 'if ...' is modified to 'if (getsockopt sd SO_KEEPALIVE)':


open Unix;;

exception Fatal of string;;

let sd = try socket PF_INET SOCK_STREAM 6 with Unix_error(e,s1,s2) -> raise(Fatal("Error:"^(error_message(e))^s1^s2)) in let opt = setsockopt sd SO_KEEPALIVE true in if (getsockopt sd SO_KEEPALIVE) = true
then print_endline("SO_KEEPALIVE") else print_endline("!SO_KEEPALIVE");;

Here is an strace of the running program:


socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3 setsockopt(3, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0 getsockopt(3, SOL_SOCKET, SO_KEEPALIVE, [8], [4]) = 0 write(1, "!SO_KEEPALIVE\n", 14!SO_KEEPALIVE ) = 14

I believe the problem is due to tha fact that the Unix getsockopt can return any non-zero integer for true (hence the [8] in the strace output). The getsockopt implementation calls getsockopt_int and returns a Val_int(..) though I believe it should return a Val_bool(..) to explictely map numbers > 1 to true.

This probably means implementing getsockopt as:


CAMLprim value unix_getsockopt_bool(value socket, value option) { return Val_bool(Int_val(getsockopt_int(sockopt_bool, socket, SOL_SOCKET, option))); }

Regards, Steve

-- Steve Bishop President - Cambridge University Caving Club - http://cucc.survex.com/

smb50@cam.ac.uk - http://smb50.quns.cam.ac.uk/ - Mobile 07748 960856 Queens' College, Cambridge. CB3 9ET

vicuna commented 21 years ago

Comment author: administrator

See #3489