caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Raising invalid_argument with a constructed string.
@ 2016-11-28 14:39 immanuel litzroth
  2016-11-28 15:43 ` David Allsopp
  2016-12-03 13:20 ` Richard W.M. Jones
  0 siblings, 2 replies; 8+ messages in thread
From: immanuel litzroth @ 2016-11-28 14:39 UTC (permalink / raw)
  To: OCaML List Mailing

[-- Attachment #1: Type: text/plain, Size: 339 bytes --]

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. Am I missing something?
Immanuel

[-- Attachment #2: Type: text/html, Size: 449 bytes --]

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

* RE: [Caml-list] Raising invalid_argument with a constructed string.
  2016-11-28 14:39 [Caml-list] Raising invalid_argument with a constructed string immanuel litzroth
@ 2016-11-28 15:43 ` David Allsopp
  2016-11-28 15:50   ` Gabriel Scherer
  2016-12-03 13:20 ` Richard W.M. Jones
  1 sibling, 1 reply; 8+ messages in thread
From: David Allsopp @ 2016-11-28 15:43 UTC (permalink / raw)
  To: immanuel litzroth, OCaML List Mailing

immanuel litzroth wrote:
> 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. Am I missing something?

Possibly - it's surprising that you want dynamic strings with an exception such as Invalid_argument which is never supposed to be caught and similarly with Failure where you shouldn't be matching on the string argument.

At a push, you can retrieve the Invalid_argument exception from caml_global_data (see byterun/fail.c), but that is essentially hacking the runtime to do so...


David

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

* Re: [Caml-list] Raising invalid_argument with a constructed string.
  2016-11-28 15:43 ` David Allsopp
@ 2016-11-28 15:50   ` Gabriel Scherer
  2016-11-29 10:21     ` immanuel litzroth
  0 siblings, 1 reply; 8+ messages in thread
From: Gabriel Scherer @ 2016-11-28 15:50 UTC (permalink / raw)
  To: David Allsopp; +Cc: immanuel litzroth, OCaML List Mailing

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?

  http://caml.inria.fr/mantis/
  https://github.com/ocaml/ocaml/pulls

On Mon, Nov 28, 2016 at 10:43 AM, David Allsopp <dra-news@metastack.com> wrote:
> immanuel litzroth wrote:
>> 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. Am I missing something?
>
> Possibly - it's surprising that you want dynamic strings with an exception such as Invalid_argument which is never supposed to be caught and similarly with Failure where you shouldn't be matching on the string argument.
>
> At a push, you can retrieve the Invalid_argument exception from caml_global_data (see byterun/fail.c), but that is essentially hacking the runtime to do so...
>
>
> David
>
> --
> 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] 8+ messages in thread

* Re: [Caml-list] Raising invalid_argument with a constructed string.
  2016-11-28 15:50   ` Gabriel Scherer
@ 2016-11-29 10:21     ` immanuel litzroth
  0 siblings, 0 replies; 8+ messages in thread
From: immanuel litzroth @ 2016-11-29 10:21 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: David Allsopp, OCaML List Mailing

[-- Attachment #1: Type: text/plain, Size: 1726 bytes --]

Done.
https://caml.inria.fr/mantis/view.php?id=7423
Cheers,
Immanuel


On Mon, Nov 28, 2016 at 4:50 PM, Gabriel Scherer <gabriel.scherer@gmail.com>
wrote:

> 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?
>
>   http://caml.inria.fr/mantis/
>   https://github.com/ocaml/ocaml/pulls
>
> On Mon, Nov 28, 2016 at 10:43 AM, David Allsopp <dra-news@metastack.com>
> wrote:
> > immanuel litzroth wrote:
> >> 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. Am I missing something?
> >
> > Possibly - it's surprising that you want dynamic strings with an
> exception such as Invalid_argument which is never supposed to be caught and
> similarly with Failure where you shouldn't be matching on the string
> argument.
> >
> > At a push, you can retrieve the Invalid_argument exception from
> caml_global_data (see byterun/fail.c), but that is essentially hacking the
> runtime to do so...
> >
> >
> > David
> >
> > --
> > 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
>

[-- Attachment #2: Type: text/html, Size: 2929 bytes --]

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

* Re: [Caml-list] Raising invalid_argument with a constructed string.
  2016-11-28 14:39 [Caml-list] Raising invalid_argument with a constructed string immanuel litzroth
  2016-11-28 15:43 ` David Allsopp
@ 2016-12-03 13:20 ` Richard W.M. Jones
  2016-12-03 14:00   ` immanuel litzroth
  1 sibling, 1 reply; 8+ messages in thread
From: Richard W.M. Jones @ 2016-12-03 13:20 UTC (permalink / raw)
  To: immanuel litzroth; +Cc: OCaML List Mailing

On Mon, Nov 28, 2016 at 03:39:05PM +0100, immanuel litzroth wrote:
> 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. Am I missing something?

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.

Rich.

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

* Re: [Caml-list] Raising invalid_argument with a constructed string.
  2016-12-03 13:20 ` Richard W.M. Jones
@ 2016-12-03 14:00   ` immanuel litzroth
  2016-12-03 19:36     ` Richard W.M. Jones
  0 siblings, 1 reply; 8+ messages in thread
From: immanuel litzroth @ 2016-12-03 14:00 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: OCaML List Mailing

[-- Attachment #1: Type: text/plain, Size: 1183 bytes --]

Yes, I've used that solution too, keeping a static fixed string around,
it's just not very nice and will break if it's ever multithreaded.
Immanuel

On Sat, Dec 3, 2016 at 2:20 PM, Richard W.M. Jones <rich@annexia.org> wrote:

> On Mon, Nov 28, 2016 at 03:39:05PM +0100, immanuel litzroth wrote:
> > 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. Am I missing something?
>
> 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.
>
> Rich.
>

[-- Attachment #2: Type: text/html, Size: 1904 bytes --]

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

* Re: [Caml-list] Raising invalid_argument with a constructed string.
  2016-12-03 14:00   ` immanuel litzroth
@ 2016-12-03 19:36     ` Richard W.M. Jones
  2016-12-03 19:44       ` Gabriel Scherer
  0 siblings, 1 reply; 8+ messages in thread
From: Richard W.M. Jones @ 2016-12-03 19:36 UTC (permalink / raw)
  To: immanuel litzroth; +Cc: OCaML List Mailing

On Sat, Dec 03, 2016 at 03:00:57PM +0100, immanuel litzroth wrote:
> Yes, I've used that solution too, keeping a static fixed string around,
> it's just not very nice and will break if it's ever multithreaded.

You shouldn't make the string static, and we do *not* do that in
libguestfs.  You should simply allocate it on the stack.  This is
thread safe.

Also on the bug it was stated:

> The stack-based solutions rely on guessing arbitrary size limits on
> the dynamic strings, which is often possibles but sometimes
> inconvenient/inelegant.

but this is not true either.  You can use alloca to allocate a
variable sized string on the stack.

As Xavier Leroy says, fixing this is not necessary, although there's
perhaps a usability argument.

Rich.

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

* Re: [Caml-list] Raising invalid_argument with a constructed string.
  2016-12-03 19:36     ` Richard W.M. Jones
@ 2016-12-03 19:44       ` Gabriel Scherer
  0 siblings, 0 replies; 8+ messages in thread
From: Gabriel Scherer @ 2016-12-03 19:44 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: immanuel litzroth, OCaML List Mailing

[-- Attachment #1: Type: text/plain, Size: 1208 bytes --]

I guess I'm not modern enough to feel comfortable using alloca (or
variable-length arrays) in my code :-)

On Sat, Dec 3, 2016 at 2:36 PM, Richard W.M. Jones <rich@annexia.org> wrote:

> On Sat, Dec 03, 2016 at 03:00:57PM +0100, immanuel litzroth wrote:
> > Yes, I've used that solution too, keeping a static fixed string around,
> > it's just not very nice and will break if it's ever multithreaded.
>
> You shouldn't make the string static, and we do *not* do that in
> libguestfs.  You should simply allocate it on the stack.  This is
> thread safe.
>
> Also on the bug it was stated:
>
> > The stack-based solutions rely on guessing arbitrary size limits on
> > the dynamic strings, which is often possibles but sometimes
> > inconvenient/inelegant.
>
> but this is not true either.  You can use alloca to allocate a
> variable sized string on the stack.
>
> As Xavier Leroy says, fixing this is not necessary, although there's
> perhaps a usability argument.
>
> Rich.
>
> --
> 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
>

[-- Attachment #2: Type: text/html, Size: 2019 bytes --]

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

end of thread, other threads:[~2016-12-03 19:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-28 14:39 [Caml-list] Raising invalid_argument with a constructed string immanuel litzroth
2016-11-28 15:43 ` David Allsopp
2016-11-28 15:50   ` Gabriel Scherer
2016-11-29 10:21     ` immanuel litzroth
2016-12-03 13:20 ` Richard W.M. Jones
2016-12-03 14:00   ` immanuel litzroth
2016-12-03 19:36     ` Richard W.M. Jones
2016-12-03 19:44       ` 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).