Skip to content

Commit

Permalink
Avoid exceptions in DESTROY for invalid instances
Browse files Browse the repository at this point in the history
Also make mpz_from_sv static.
  • Loading branch information
rafl committed Sep 20, 2010
1 parent 3ad60f4 commit 9a81110
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
27 changes: 21 additions & 6 deletions GMP.xs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "XSUB.h"
#include "gmp.h"

typedef mpz_t mpz_t_ornull;

/* for Perl prior to v5.7.1 */
#ifndef SvUOK
# define SvUOK(sv) SvIOK_UV(sv)
Expand Down Expand Up @@ -104,8 +106,8 @@ sv_from_mpz (mpz_t *mpz)
return obj;
}

mpz_t *
mpz_from_sv (SV *sv)
STATIC mpz_t *
mpz_from_sv_nofail (SV *sv)
{
MAGIC *mg;

Expand All @@ -126,7 +128,18 @@ mpz_from_sv (SV *sv)
}
}

croak("failed to fetch mpz pointer");
return (mpz_t *)NULL;
}

STATIC mpz_t *
mpz_from_sv (SV *sv)
{
mpz_t *mpz;

if (!(mpz = mpz_from_sv_nofail(sv)))
croak("failed to fetch mpz pointer");

return mpz;
}

/*
Expand Down Expand Up @@ -275,11 +288,13 @@ _1ex(Class,x)

void
DESTROY(n)
mpz_t* n
mpz_t_ornull* n

PPCODE:
mpz_clear(*n);
free(n);
if (n) {
mpz_clear(*n);
free(n);
}

##############################################################################
# _num() - numify, return string so that atof() and atoi() can use it
Expand Down
3 changes: 3 additions & 0 deletions typemap
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mpz_t * MPZ
mpz_t_ornull * MPZ_NF

INPUT
MPZ
$var = mpz_from_sv($arg);
MPZ_NF
$var = mpz_from_sv_nofail($arg);

OUTPUT
MPZ
Expand Down

0 comments on commit 9a81110

Please sign in to comment.