Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007423OCamlruntime system and C interfacepublic2016-11-29 11:192016-12-27 10:18
Reportersdev 
Assigned Toshinwell 
PrioritynormalSeverityfeatureReproducibilityalways
StatusclosedResolutionduplicate 
PlatformLinuxOSUbuntuOS Version14.04
Product Version4.03.0 
Target VersionFixed in Version 
Summary0007423: caml_invalid_argument can't be used with dynamically allocated strings
DescriptionFrom https://sympa.inria.fr/sympa/arc/caml-list/2016-11/msg00106.html [^]

caml_invalid_argument(str) is no return and does not free it's
argument. So calling it with a string constructed dynamically will mean it'll never get freed.
I could construct an ocaml string and pass that to caml_raise_with_arg,
but I don't seem to be able to get to the caml_exn_Invalid_argument from
c.

Reply from Gabriel Scherer:
It may make sense to have caml_{failwith,invalid_argument}_value
variants of the exception-raising functions that take a parameter, and
be implemented using caml_raise_with_arg(s) directly instead of
caml_raise_with_string. Could you open a mantis issue or submit a
github pull request to track the question and continue discussion?
Steps To Reproducechar* error = new char[50];
snprintf(error, 50, "error: %d", 50);
caml_invalid_argument(error)

# allocated memory is lost here.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0016627)
gasche (developer)
2016-12-03 18:08

Patch proposed in https://github.com/ocaml/ocaml/pull/946 [^]
(0016628)
xleroy (administrator)
2016-12-03 19:56

I'm not convinced by the repro case (just declare "char error[50];") nor by the need in general.
(0016630)
gasche (developer)
2016-12-03 20:02

Richard W. Jones mentioned that he could use a similar feature on the mailing-list: https://sympa.inria.fr/sympa/arc/caml-list/2016-12/msg00021.html [^]


> We have the same problem in libguestfs in a few places. The solution
> is to use an allocation on the C stack, either a fixed size buffer or
> [although we don't currently use this] a variable sized one using
> alloca. The string is freed when the stack is unwound. Examples:
>
> https://github.com/libguestfs/libguestfs/blob/master/builder/yajl-c.c#L108-L114 [^]
> https://github.com/libguestfs/libguestfs/blob/master/v2v/domainxml-c.c#L120 [^]
>
> Be nice to have a "freeing" version of caml_raise* I suppose.

The stack-based solutions rely on guessing arbitrary size limits on the dynamic strings, which is often possibles but sometimes inconvenient/inelegant.
(0017046)
shinwell (developer)
2016-12-27 10:18

Moving discussion to here: https://github.com/ocaml/ocaml/pull/946 [^]

- Issue History
Date Modified Username Field Change
2016-11-29 11:19 sdev New Issue
2016-12-03 18:08 gasche Note Added: 0016627
2016-12-03 19:56 xleroy Note Added: 0016628
2016-12-03 20:02 gasche Note Added: 0016630
2016-12-06 15:41 frisch Severity minor => feature
2016-12-08 10:42 shinwell Status new => acknowledged
2016-12-27 10:18 shinwell Note Added: 0017046
2016-12-27 10:18 shinwell Status acknowledged => closed
2016-12-27 10:18 shinwell Assigned To => shinwell
2016-12-27 10:18 shinwell Resolution open => duplicate
2017-02-23 16:43 doligez Category OCaml runtime system => runtime system
2017-03-03 17:45 doligez Category runtime system => runtime system and C interface


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker