Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Big_int.extract_big_int does not handle negative numbers properly #6010

Closed
vicuna opened this issue May 7, 2013 · 1 comment
Closed

Big_int.extract_big_int does not handle negative numbers properly #6010

vicuna opened this issue May 7, 2013 · 1 comment
Assignees

Comments

@vicuna
Copy link

@vicuna vicuna commented May 7, 2013

Original bug ID: 6010
Reporter: @glondu
Assigned to: @xavierleroy
Status: closed (set by @xavierleroy on 2015-12-11T18:18:51Z)
Resolution: fixed
Priority: normal
Severity: major
Version: 4.00.1
Category: otherlibs

Bug description

This bug has been originally reported in Debian by Drake Wilson at:

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=707127

Big_int.extract_big_int does not handle negative numbers properly.

This function seems to assume that unrepresented prefix regions of
bits in a Big_int.big_int are always zero, but this is not so for
negative numbers. The documentation explicitly states that a two's
complement representation is used for those, but see the transcript
with comments inline.

Steps to reproduce

% ocaml nums.cma
Objective Caml version 3.12.1

open Big_int;;

let mu = minus_big_int unit_big_int;;

val mu : Big_int.big_int =

let s offset count = string_of_big_int (extract_big_int mu offset count);;

val s : int -> int -> string =

s 0 16;;

  • : string = "65535"

s 16 16;;

  • : string = "65535"

s 32 16;;

  • : string = "65535"

s 48 16;;

  • : string = "65535"
    (* Okay so far... *)

s 64 16;;

  • : string = "0"
    (* Oops! *)

s 56 16;;

  • : string = "255"
    (* Function seems to think this is a 2^64-1. *)

eq_big_int (big_int_of_int 255) (extract_big_int mu 56 16);;

  • : bool = true
    (* It's not just a problem with the stringifier. *)

eq_big_int (big_int_of_int (-1)) mu;;

  • : bool = true
    (* Nor is it a problem with the earlier negation. *)

Additional information

Same behaviour with OCaml version 4.00.1.

@vicuna
Copy link
Author

@vicuna vicuna commented May 8, 2013

Comment author: @xavierleroy

Fixed in trunk, commit 13660.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants