do not allocate or access zero sized arrays (#19983)
* do not allocate or access zero sized arrays * do not access zero dimensions or points
Costa Tsaousis committed
Mar 27, 2025 at 16:15 UTC
3e18d2dcd70d2335c9217adea314b483674626e2
2 files changed
+40
-29
src/web/api/formatters/value/value.c
+3
@@ -5,6 +5,9 @@
5
inline NETDATA_DOUBLE rrdr2value(RRDR *r, long i, RRDR_OPTIONS options, int *all_values_are_null, NETDATA_DOUBLE *anomaly_rate) {
6
size_t c;
7
8
+ if(!r->d || !r->n || (size_t)i >= r->n)
9
+ return NAN;
10
+
11
NETDATA_DOUBLE *cn = &r->v[ i * r->d ];
12
RRDR_VALUE_FLAGS *co = &r->o[ i * r->d ];
13
NETDATA_DOUBLE *ar = &r->ar[ i * r->d ];
src/web/api/queries/query.c
+37
-29
@@ -2827,25 +2827,31 @@ static RRDR *rrd2rrdr_group_by_initialize(ONEWAYALLOC *owa, QUERY_TARGET *qt) {
2827
last_r = r;
2828
2829
rrd2rrdr_set_timestamps(r);
2830
- r->dp = onewayalloc_callocz(owa, r->d, sizeof(*r->dp));
2831
- r->dview = onewayalloc_callocz(owa, r->d, sizeof(*r->dview));
2832
- r->dgbc = onewayalloc_callocz(owa, r->d, sizeof(*r->dgbc));
2833
- r->gbc = onewayalloc_callocz(owa, r->n * r->d, sizeof(*r->gbc));
2834
- r->dqp = onewayalloc_callocz(owa, r->d, sizeof(STORAGE_POINT));
2830
2836
- if(hidden_dimensions && ((group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE) || (aggregation_method == RRDR_GROUP_BY_FUNCTION_PERCENTAGE)))
2837
- // this is where we are going to group the hidden dimensions
2838
- r->vh = onewayalloc_mallocz(owa, r->n * r->d * sizeof(*r->vh));
2831
+ if(r->d) {
2832
+ r->dp = onewayalloc_callocz(owa, r->d, sizeof(*r->dp));
2833
+ r->dview = onewayalloc_callocz(owa, r->d, sizeof(*r->dview));
2834
+ r->dgbc = onewayalloc_callocz(owa, r->d, sizeof(*r->dgbc));
2835
+ r->dqp = onewayalloc_callocz(owa, r->d, sizeof(STORAGE_POINT));
2836
+
2837
+ if(!final_grouping)
2838
+ // this is where we are going to store the slot in the next RRDR
2839
+ // that we are going to group by the dimension of this RRDR
2840
+ r->dgbs = onewayalloc_callocz(owa, r->d, sizeof(*r->dgbs));
2841
+
2842
+ if (label_keys) {
2843
+ r->dl = onewayalloc_callocz(owa, r->d, sizeof(DICTIONARY *));
2844
+ r->label_keys = label_keys;
2845
+ label_keys = NULL;
2846
+ }
2847
2840
- if(!final_grouping)
2841
- // this is where we are going to store the slot in the next RRDR
2842
- // that we are going to group by the dimension of this RRDR
2843
- r->dgbs = onewayalloc_callocz(owa, r->d, sizeof(*r->dgbs));
2848
+ if(r->n) {
2849
+ r->gbc = onewayalloc_callocz(owa, r->n * r->d, sizeof(*r->gbc));
2850
2845
- if (label_keys) {
2846
- r->dl = onewayalloc_callocz(owa, r->d, sizeof(DICTIONARY *));
2847
- r->label_keys = label_keys;
2848
- label_keys = NULL;
2851
+ if(hidden_dimensions && ((group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE) || (aggregation_method == RRDR_GROUP_BY_FUNCTION_PERCENTAGE)))
2852
+ // this is where we are going to group the hidden dimensions
2853
+ r->vh = onewayalloc_mallocz(owa, r->n * r->d * sizeof(*r->vh));
2854
+ }
2855
}
2856
2857
// zero r (dimension options, names, and ids)
@@ -2873,19 +2879,21 @@ static RRDR *rrd2rrdr_group_by_initialize(ONEWAYALLOC *owa, QUERY_TARGET *qt) {
2879
r->partial_data_trimming.trimmed_after = qt->window.before;
2880
2881
// make all values empty
2876
- for (size_t i = 0; i != r->n; i++) {
2877
- NETDATA_DOUBLE *cn = &r->v[i * r->d];
2878
- RRDR_VALUE_FLAGS *co = &r->o[i * r->d];
2879
- NETDATA_DOUBLE *ar = &r->ar[i * r->d];
2880
- NETDATA_DOUBLE *vh = r->vh ? &r->vh[i * r->d] : NULL;
2881
-
2882
- for (size_t d = 0; d < r->d; d++) {
2883
- cn[d] = NAN;
2884
- ar[d] = 0.0;
2885
- co[d] = RRDR_VALUE_EMPTY;
2886
-
2887
- if(vh)
2888
- vh[d] = NAN;
2882
+ if(r->n && r->d) {
2883
+ for (size_t i = 0; i != r->n; i++) {
2884
+ NETDATA_DOUBLE *cn = &r->v[i * r->d];
2885
+ RRDR_VALUE_FLAGS *co = &r->o[i * r->d];
2886
+ NETDATA_DOUBLE *ar = &r->ar[i * r->d];
2887
+ NETDATA_DOUBLE *vh = r->vh ? &r->vh[i * r->d] : NULL;
2888
+
2889
+ for (size_t d = 0; d < r->d; d++) {
2890
+ cn[d] = NAN;
2891
+ ar[d] = 0.0;
2892
+ co[d] = RRDR_VALUE_EMPTY;
2893
+
2894
+ if (vh)
2895
+ vh[d] = NAN;
2896
+ }
2897
}
2898
}
2899
}