Permalink
Browse files

Avoid exceptions in DESTROY for invalid instances

Also make mpz_from_sv static.
  • Loading branch information...
1 parent 3ad60f4 commit 9a81110655d2a9171487a04c309afe213eaf7c80 @rafl committed Sep 20, 2010
Showing with 24 additions and 6 deletions.
  1. +21 −6 GMP.xs
  2. +3 −0 typemap
View
@@ -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)
@@ -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;
@@ -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;
}
/*
@@ -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
View
@@ -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

0 comments on commit 9a81110

Please sign in to comment.