[GiNaC-devel] [PATCH 6/6] shaker_sort, permutation_sign: fix invalid use of STL iterators.

Alexei Sheplyakov varg at metalica.kh.ua
Sat Aug 8 12:03:48 CEST 2009


According to the standard incrementing end(), and decrementing begin()
are ill-defined operations (see [lib.forward.iterators],
[lib.bidirectional.iterators]).
---
 ginac/utils.h |   24 ++++++++++++++++++------
 1 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/ginac/utils.h b/ginac/utils.h
index 393c474..e65bdae 100644
--- a/ginac/utils.h
+++ b/ginac/utils.h
@@ -128,7 +128,9 @@ int permutation_sign(It first, It last)
 				sign = -sign;
 			} else if (!(*other < *i))
 				return 0;
-			--i; --other;
+			--i;
+			if (i != first)
+				--other;
 		}
 		if (!swapped)
 			return sign;
@@ -147,7 +149,9 @@ int permutation_sign(It first, It last)
 				sign = -sign;
 			} else if (!(*i < *other))
 				return 0;
-			++i; ++other;
+			++i;
+			if (i != last)
+				++other;
 		}
 		if (!swapped)
 			return sign;
@@ -181,7 +185,9 @@ int permutation_sign(It first, It last, Cmp comp, Swap swapit)
 				sign = -sign;
 			} else if (!comp(*other, *i))
 				return 0;
-			--i; --other;
+			--i;
+			if (i != first)
+				--other;
 		}
 		if (!swapped)
 			return sign;
@@ -200,7 +206,9 @@ int permutation_sign(It first, It last, Cmp comp, Swap swapit)
 				sign = -sign;
 			} else if (!comp(*i, *other))
 				return 0;
-			++i; ++other;
+			++i; 
+			if (i != last)
+				++other;
 		}
 		if (!swapped)
 			return sign;
@@ -232,7 +240,9 @@ void shaker_sort(It first, It last, Cmp comp, Swap swapit)
 				flag = other;
 				swapped = true;
 			}
-			--i; --other;
+			--i;
+			if (i != first)
+				--other;
 		}
 		if (!swapped)
 			return;
@@ -249,7 +259,9 @@ void shaker_sort(It first, It last, Cmp comp, Swap swapit)
 				flag = other;
 				swapped = true;
 			}
-			++i; ++other;
+			++i;
+			if (i != last)
+				++other;
 		}
 		if (!swapped)
 			return;
-- 
1.6.3.3



More information about the GiNaC-devel mailing list