[GRASS-SVN] r69774 - grass/trunk/raster/r.quantile

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Nov 5 14:20:25 PDT 2016


Author: mmetz
Date: 2016-11-05 14:20:24 -0700 (Sat, 05 Nov 2016)
New Revision: 69774

Modified:
   grass/trunk/raster/r.quantile/main.c
Log:
r.quantile: fix memory violations

Modified: grass/trunk/raster/r.quantile/main.c
===================================================================
--- grass/trunk/raster/r.quantile/main.c	2016-11-04 15:17:49 UTC (rev 69773)
+++ grass/trunk/raster/r.quantile/main.c	2016-11-05 21:20:24 UTC (rev 69774)
@@ -52,6 +52,9 @@
 
 static inline double get_quantile(int n)
 {
+    if (n >= num_quants)
+	return (double)total + total;
+
     return (double)total * quants[n];
 }
 
@@ -103,7 +106,8 @@
 	unsigned int count = slots[slot];
 	unsigned long accum2 = accum + count;
 
-	if (accum2 > next) {
+	if (accum2 > next ||
+	    (slot == num_slots - 1 && accum2 == next)) {
 	    struct bin *b = &bins[bin];
 
 	    slot_bins[slot] = ++bin;
@@ -196,7 +200,7 @@
     int quant;
 
     for (quant = 0; quant < num_quants; quant++) {
-	struct bin *b;
+	struct bin *b = &bins[bin];
 	double next = get_quantile(quant);
 	double k, v;
 	int i0, i1;
@@ -212,6 +216,8 @@
 	    i0 = (int)floor(k);
 	    i1 = (int)ceil(k);
 
+	    if (i0 > b->count - 1)
+		i0 = b->count - 1;
 	    if (i1 > b->count - 1)
 		i1 = b->count - 1;
 



More information about the grass-commit mailing list