Skip to content

Commit 7f40cbd

Browse files
committed
lib/arraystats: re-enable "Discont" algorithm
Addresses heap buffer overflow in previously disabled 'Discont' algorithm. The immediate cause was too small allocation of variable `num` in `AS_class_discont()`, which was fixed by an increase it's size (broadly mirroring the original Fortran code). This fix was complemented by some code related improvements: - Code clean up and restructure of `AS_class_discont()` - Re-enable "dis" algorithm in v.class and d.vect.thematic - Pass `classbreaks` array by pointer to pointer to enable in- and output. See: https://lists.osgeo.org/pipermail/grass-dev/2008-July/038951.html Fixes: OSGeo#5486
1 parent f48be67 commit 7f40cbd

File tree

5 files changed

+150
-153
lines changed

5 files changed

+150
-153
lines changed

display/d.vect.thematic/main.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,10 @@ int main(int argc, char **argv)
119119
algo_opt->options = "int,std,qua,equ,dis";
120120
algo_opt->description = _("Algorithm to use for classification");
121121
desc = NULL;
122-
G_asprintf(&desc, "int;%s;std;%s;qua;%s;equ;%s", _("simple intervals"),
123-
_("standard deviations"), _("quantiles"),
124-
_("equiprobable (normal distribution)"));
122+
G_asprintf(&desc, "int;%s;std;%s;qua;%s;equ;%s;dis;%s",
123+
_("simple intervals"), _("standard deviations"), _("quantiles"),
124+
_("equiprobable (normal distribution)"), _("discontinuities"));
125125
algo_opt->descriptions = desc;
126-
/*currently disabled because of bugs "dis;discontinuities"); */
127126
algo_opt->guisection = _("Classes");
128127

129128
nbclass_opt = G_define_option();
@@ -415,7 +414,7 @@ int main(int argc, char **argv)
415414
* algorithms */
416415
class_info =
417416
AS_class_apply_algorithm(AS_option_to_algorithm(algo_opt), data,
418-
nrec, &nbreaks, breakpoints);
417+
nrec, &nbreaks, &breakpoints);
419418
}
420419
else {
421420

include/grass/defs/arraystats.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
#define GRASS_ARRAYSTATSDEFS_H
33

44
/* basic.c */
5-
void AS_eqdrt(double[], double[], int, int, double *);
5+
void AS_eqdrt(double[], double[], int, int, double *, double *, double *);
66
void AS_basic_stats(double *, int, struct GASTATS *);
77

88
/* class.c */
99
int AS_option_to_algorithm(const struct Option *);
10-
double AS_class_apply_algorithm(int, double *, int, int *, double *);
11-
int AS_class_interval(double *, int, int, double *);
12-
int AS_class_quant(double *, int, int, double *);
13-
double AS_class_discont(double *, int, int, double *);
14-
double AS_class_stdev(double *, int, int, double *);
15-
int AS_class_equiprob(double *, int, int *, double *);
10+
double AS_class_apply_algorithm(int, double *, int, int *, double **);
11+
int AS_class_interval(double *, int, int, double **);
12+
int AS_class_quant(double *, int, int, double **);
13+
double AS_class_discont(double *, int, int, double **);
14+
double AS_class_stdev(double *, int, int, double **);
15+
int AS_class_equiprob(double *, int, int *, double **);
1616
int AS_class_frequencies(double *, int, int, double *, int *);
1717

1818
#endif

lib/arraystats/basic.c

+32-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <math.h>
2+
23
#include <grass/arraystats.h>
4+
#include <grass/gis.h>
35

46
/*provides basic univar stats */
57
void AS_basic_stats(double *data, int count, struct GASTATS *stats)
@@ -34,31 +36,43 @@ void AS_basic_stats(double *data, int count, struct GASTATS *stats)
3436
return;
3537
}
3638

37-
void AS_eqdrt(double vectx[], double vecty[], int i1, int i2, double *vabc)
39+
void AS_eqdrt(double vectx[], double vecty[], int i1, int i2, double *a,
40+
double *b, double *c)
3841
{
39-
double bn = 0, bd = 0, x1 = 0, y1 = 0;
42+
double bn = 0.0, bd = 0.0, x1 = 0.0, y1 = 0.0;
43+
double x2 = 0.0, y2 = 0.0;
44+
45+
*a = 0.0;
46+
*b = 0.0;
47+
*c = 0.0;
48+
49+
x1 = vectx[i1];
50+
y1 = vecty[i1];
51+
x2 = vectx[i2];
52+
y2 = vecty[i2];
4053

41-
vabc[0] = 0;
42-
vabc[1] = 0;
43-
vabc[2] = 0;
4454
if (i1 == 0) {
45-
x1 = 0;
46-
y1 = 0;
55+
x1 = 0.0;
56+
y1 = 0.0;
4757
}
4858
else {
4959
x1 = vectx[i1];
5060
y1 = vecty[i1];
5161
}
52-
bn = y1 - vecty[i2];
53-
bd = x1 - vectx[i2];
54-
if (bd != 0) {
55-
vabc[1] = bn / bd;
56-
vabc[0] = y1 - vabc[1] * x1;
57-
return;
62+
63+
bn = y1 - y2;
64+
bd = x1 - x2;
65+
66+
if (fabs(bd) < GRASS_EPSILON) {
67+
if (fabs(bn) < GRASS_EPSILON) {
68+
printf("Points are equal\n");
69+
}
70+
else {
71+
*c = x1;
72+
}
73+
}
74+
else {
75+
*b = bn / bd;
76+
*a = y1 - *b * x1;
5877
}
59-
if (bn != 0)
60-
vabc[2] = x1;
61-
else
62-
G_debug(3, "Points are equal\n");
63-
return;
6478
}

0 commit comments

Comments
 (0)