Index: /trunk/psLib/src/types/psMetadata.c
===================================================================
--- /trunk/psLib/src/types/psMetadata.c	(revision 12824)
+++ /trunk/psLib/src/types/psMetadata.c	(revision 12825)
@@ -12,6 +12,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.158 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-03-23 23:25:02 $
+ *  @version $Revision: 1.159 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-04-13 00:22:35 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -597,6 +597,6 @@
 
     // how the item is added to the hash depends on prior existence, flags, etc.
-    if(existingEntry != NULL) { // prior existence
-        if (existingEntry->type == PS_DATA_METADATA_MULTI || (flags & PS_META_DUPLICATE_OK) != 0) {
+    if (existingEntry) { // prior existence
+        if (existingEntry->type == PS_DATA_METADATA_MULTI || (flags & PS_META_DUPLICATE_OK)) {
             // duplicate entries allowed - add another entry.
 
@@ -605,5 +605,5 @@
 
             // add to the hash's list of duplicate entries
-            if (! psListAdd(existingEntry->data.list, PS_LIST_TAIL, (psPtr)item) ) {
+            if (!psListAdd(existingEntry->data.list, PS_LIST_TAIL, (psPtr)item) ) {
                 psError(PS_ERR_UNKNOWN, false,
                         _("Failed to add metadata item, %s, to metadata collection list."),
@@ -611,5 +611,5 @@
                 return false;
             }
-        } else if ((flags & PS_META_REPLACE) != 0) {
+        } else if (flags & PS_META_REPLACE) {
             // replace entry instead of creating a duplicate entry.
 
@@ -618,24 +618,13 @@
                 return false;
             }
-
-            // remove the existing entry from metadata
-            psMetadataRemoveKey(md, key);
-
-            // treat as if new (added to list below)
-            /*  The following is unneeded.  HashAdd can't return false with non-null inputs.
-                        if(!psHashAdd(mdTable, key, (psPtr)item)) {
-                            psError(PS_ERR_UNKNOWN,false,
-                                _("Failed to add metadata item, %s, to items table."),key);
-                            return false;
-                        }
-            */
-            psHashAdd(mdTable, key, (psPtr)item);
-
-            // Generate warning that item has been replaced
-            //            psLogMsg(__func__,PS_LOG_WARN,"Metadata item %s has been replaced",
-            //                     existingEntry->name);
+            if (item != existingEntry) {
+                // Replacing an item with itself can lead to memory corruption
+                // when you blow away what you're trying to add
+                psMetadataRemoveKey(md, key);
+                psHashAdd(mdTable, key, (psPtr)item);
+            }
         } else {
             // default is to error on duplicate entry.
-            if ((flags & PS_META_NO_REPLACE) != 0) {
+            if (flags & PS_META_NO_REPLACE) {
                 return true;
             }
