OSDN Git Service

Some parameter editing caused segmentation fault; fixed.
authortoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Thu, 6 May 2010 15:13:59 +0000 (15:13 +0000)
committertoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Thu, 6 May 2010 15:13:59 +0000 (15:13 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/molby/trunk@45 a2be9bc6-48de-4e38-9406-05402d4bc13c

MolLib/MolAction.c
MolLib/Parameter.c
MolLib/Parameter.h

index 39fe8d9..78e6969 100644 (file)
@@ -559,7 +559,7 @@ sMolActionUpdateSelectionAndParameterNumbering(Molecule *mol, const IntGroup *ig
                        if (!is_insert)
                                ig3 = IntGroupNew();
                        for (j = 0; (up = ParameterGetUnionParFromTypeAndIndex(mol->par, i, j)) != NULL; j++) {
-                               usave = *up;
+                               ParameterCopyOneWithType(&usave, up, i);  /*  Don't say usave = *up  */
                                if (ParameterRenumberAtoms(i, up, old_natoms, ip) && !is_insert) {
                                        IntGroupAdd(ig3, j, 1);  /*  This parameter is to be restored on undo  */
                                        AssignArray(&upary, &count_upary, sizeof(UnionPar), count_upary, &usave);
@@ -780,7 +780,7 @@ MolActionPerform(Molecule *mol, MolAction *action)
                ip = (Int *)malloc(sizeof(Int) * 2 * result);
                if (ip == NULL)
                        return -4;
-               memmove(ip, mol->bonds - result * 2, sizeof(Int) * 2 * result);
+               memmove(ip, mol->bonds + (mol->nbonds - result) * 2, sizeof(Int) * 2 * result);
                act2 = MolActionNew(gMolActionDeleteBonds, result * 2, ip);
                free(ip);
                needsRebuildMDArena = 1;
index c14eac2..7a6890f 100755 (executable)
@@ -283,6 +283,13 @@ ParameterGetSizeForType(int type)
        }
 }
 
+void
+ParameterCopyOneWithType(UnionPar *dst, const UnionPar *src, int type)
+{
+       int size = ParameterGetSizeForType(type);
+       memmove(dst, src, size);
+}
+
 UnionPar *
 ParameterRefGetPar(ParameterRef *pref)
 {
index 7f98d4e..92b3871 100755 (executable)
@@ -202,6 +202,12 @@ int ParameterInsert(Parameter *par, Int type, const UnionPar *up, struct IntGrou
 int ParameterDelete(Parameter *par, Int type, UnionPar *up, struct IntGroup *where);
 int ParameterCopy(Parameter *par, Int type, UnionPar *up, struct IntGroup *where);
 
+/*  Caution!  When up is a UnionPar pointer given by ParameterGetUnionParFromTypeAndIndex() etc and
+    u is a UnionPar variable, u = *up will cause Bad Address exception, if up is at the last
+    of the allocated array and sizeof(UnionPar) is larger than the size of actual parameter record.
+    This copy function does take care of such case.  */
+void ParameterCopyOneWithType(UnionPar *dst, const UnionPar *src, int type);
+
 int ParameterRenumberAtoms(Int type, UnionPar *up, Int oldnatoms, const Int *old2new);
 int ParameterDoesContainAtom(Int type, UnionPar *up, UInt atom_type, Int options);