[PATCH 5.004_60] Fix to MM_VMS.PM
[p5sagit/p5-mst-13.2.git] / hv.c
diff --git a/hv.c b/hv.c
index add7a39..b1e095a 100644 (file)
--- a/hv.c
+++ b/hv.c
@@ -43,7 +43,7 @@ more_he(void)
 {
     register HE* he;
     register HE* heend;
-    he_root = (HE*)safemalloc(1008);
+    New(54, he_root, 1008/sizeof(HE), HE);
     he = he_root;
     heend = &he[1008 / sizeof(HE) - 1];
     while (he < heend) {
@@ -257,7 +257,6 @@ hv_magic_check (HV *hv, bool *needs_copy, bool *needs_store)
            *needs_copy = TRUE;
            switch (mg->mg_type) {
            case 'P':
-           case 'I':
            case 'S':
                *needs_store = FALSE;
            }
@@ -424,26 +423,33 @@ hv_delete(HV *hv, char *key, U32 klen, I32 flags)
     register U32 hash;
     register HE *entry;
     register HE **oentry;
+    SV **svp;
     SV *sv;
 
     if (!hv)
        return Nullsv;
     if (SvRMAGICAL(hv)) {
-       sv = *hv_fetch(hv, key, klen, TRUE);
-       mg_clear(sv);
-       if (mg_find(sv, 's')) {
-           return Nullsv;              /* %SIG elements cannot be deleted */
-       }
-       else if (mg_find(sv, 'p')) {
-           sv_unmagic(sv, 'p');        /* No longer an element */
-           return sv;
-       }
+       bool needs_copy;
+       bool needs_store;
+       hv_magic_check (hv, &needs_copy, &needs_store);
+
+       if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
+           sv = *svp;
+           mg_clear(sv);
+           if (!needs_store) {
+               if (mg_find(sv, 'p')) {
+                   sv_unmagic(sv, 'p');        /* No longer an element */
+                   return sv;
+               }
+               return Nullsv;          /* element cannot be deleted */
+           }
 #ifdef ENV_IS_CASELESS
-       else if (mg_find((SV*)hv,'E')) {
-           sv = sv_2mortal(newSVpv(key,klen));
-           key = strupr(SvPVX(sv));
-       }
+           else if (mg_find((SV*)hv,'E')) {
+               sv = sv_2mortal(newSVpv(key,klen));
+               key = strupr(SvPVX(sv));
+           }
 #endif
+        }
     }
     xhv = (XPVHV*)SvANY(hv);
     if (!xhv->xhv_array)
@@ -492,21 +498,29 @@ hv_delete_ent(HV *hv, SV *keysv, I32 flags, U32 hash)
     if (!hv)
        return Nullsv;
     if (SvRMAGICAL(hv)) {
-       entry = hv_fetch_ent(hv, keysv, TRUE, hash);
-       sv = HeVAL(entry);
-       mg_clear(sv);
-       if (mg_find(sv, 'p')) {
-           sv_unmagic(sv, 'p');        /* No longer an element */
-           return sv;
-       }
+       bool needs_copy;
+       bool needs_store;
+       hv_magic_check (hv, &needs_copy, &needs_store);
+
+       if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
+           sv = HeVAL(entry);
+           mg_clear(sv);
+           if (!needs_store) {
+               if (mg_find(sv, 'p')) {
+                   sv_unmagic(sv, 'p');        /* No longer an element */
+                   return sv;
+               }               
+               return Nullsv;          /* element cannot be deleted */
+           }
 #ifdef ENV_IS_CASELESS
-       else if (mg_find((SV*)hv,'E')) {
-           key = SvPV(keysv, klen);
-           keysv = sv_2mortal(newSVpv(key,klen));
-           (void)strupr(SvPVX(keysv));
-           hash = 0; 
-       }
+           else if (mg_find((SV*)hv,'E')) {
+               key = SvPV(keysv, klen);
+               keysv = sv_2mortal(newSVpv(key,klen));
+               (void)strupr(SvPVX(keysv));
+               hash = 0; 
+           }
 #endif
+       }
     }
     xhv = (XPVHV*)SvANY(hv);
     if (!xhv->xhv_array)
@@ -662,6 +676,10 @@ hsplit(HV *hv)
     nomemok = TRUE;
 #ifdef STRANGE_MALLOC
     Renew(a, newsize, HE*);
+    if (!a) {
+      nomemok = FALSE;
+      return;
+    }
 #else
     i = newsize * sizeof(HE*);
 #define MALLOC_OVERHEAD 16
@@ -672,6 +690,10 @@ hsplit(HV *hv)
     tmp /= sizeof(HE*);
     assert(tmp >= newsize);
     New(2,a, tmp, HE*);
+    if (!a) {
+      nomemok = FALSE;
+      return;
+    }
     Copy(xhv->xhv_array, a, oldsize, HE*);
     if (oldsize >= 64) {
        offer_nice_chunk(xhv->xhv_array,
@@ -735,6 +757,10 @@ hv_ksplit(HV *hv, IV newmax)
        nomemok = TRUE;
 #ifdef STRANGE_MALLOC
        Renew(a, newsize, HE*);
+        if (!a) {
+         nomemok = FALSE;
+         return;
+       }
 #else
        i = newsize * sizeof(HE*);
        j = MALLOC_OVERHEAD;
@@ -744,6 +770,10 @@ hv_ksplit(HV *hv, IV newmax)
        j /= sizeof(HE*);
        assert(j >= newsize);
        New(2, a, j, HE*);
+        if (!a) {
+         nomemok = FALSE;
+         return;
+       }
        Copy(xhv->xhv_array, a, oldsize, HE*);
        if (oldsize >= 64) {
            offer_nice_chunk(xhv->xhv_array,