瀏覽代碼

r.in.lidar: cast to size_t before multiplying rows and cols

Otherwise it segfaults with number of cells larger
than max of signed int.


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@68083 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 9 年之前
父節點
當前提交
0b8a49fac7
共有 3 個文件被更改,包括 19 次插入18 次删除
  1. 1 1
      raster/r.in.lidar/main.c
  2. 17 16
      raster/r.in.lidar/point_binning.c
  3. 1 1
      raster/r.in.lidar/support.c

+ 1 - 1
raster/r.in.lidar/main.c

@@ -551,7 +551,7 @@ int main(int argc, char *argv[])
         if (base_array) {
             G_debug(2, "filling base raster array");
             for (row = 0; row < rows; row++) {
-                Rast_get_row(base_raster, base_array + (row * cols * Rast_cell_size(base_raster_data_type)), row, base_raster_data_type);
+                Rast_get_row(base_raster, base_array + ((size_t) row * cols * Rast_cell_size(base_raster_data_type)), row, base_raster_data_type);
             }
         }
 

+ 17 - 16
raster/r.in.lidar/point_binning.c

@@ -89,7 +89,7 @@ int update_bin_index(struct BinIndex *bin_index, void *index_array,
 
     ptr =
         G_incr_void_ptr(ptr,
-                        ((row * cols) + col) * Rast_cell_size(CELL_TYPE));
+                        (((size_t) row * cols) + col) * Rast_cell_size(CELL_TYPE));
 
     /* first node */
     if (Rast_is_null_value(ptr, CELL_TYPE)) {
@@ -234,6 +234,7 @@ int check_rows_cols_fit_to_size_t(int rows, int cols)
     if (sizeof(size_t) < 8) {
         double dsize = rows * (cols + 1);
 
+        /* TODO: the comparison with double may fail */
         if (dsize != (size_t) rows * (cols + 1))
             return FALSE;
     }
@@ -380,8 +381,8 @@ void write_variance(void *raster_row, void *n_array, void *sum_array,
     void *ptr = raster_row;
 
     for (col = 0; col < cols; col++) {
-        offset = (row * cols + col) * Rast_cell_size(rtype);
-        n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
+        offset = ((size_t) row * cols + col) * Rast_cell_size(rtype);
+        n_offset = ((size_t) row * cols + col) * Rast_cell_size(CELL_TYPE);
         n = Rast_get_c_value(n_array + n_offset, CELL_TYPE);
         sum = Rast_get_d_value(sum_array + offset, rtype);
         sumsq = Rast_get_d_value(sumsq_array + offset, rtype);
@@ -430,7 +431,7 @@ void write_median(struct BinIndex *bin_index, void *raster_row,
     void *ptr = raster_row;
 
     for (col = 0; col < cols; col++) {
-        n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
+        n_offset = ((size_t) row * cols + col) * Rast_cell_size(CELL_TYPE);
         if (Rast_is_null_value(index_array + n_offset, CELL_TYPE))      /* no points in cell */
             Rast_set_null_value(ptr, 1, rtype);
         else {                  /* one or more points in cell */
@@ -486,7 +487,7 @@ void write_percentile(struct BinIndex *bin_index, void *raster_row,
     void *ptr = raster_row;
 
     for (col = 0; col < cols; col++) {
-        n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
+        n_offset = ((size_t) row * cols + col) * Rast_cell_size(CELL_TYPE);
         if (Rast_is_null_value(index_array + n_offset, CELL_TYPE))      /* no points in cell */
             Rast_set_null_value(ptr, 1, rtype);
         else {
@@ -541,7 +542,7 @@ void write_skewness(struct BinIndex *bin_index, void *raster_row,
     void *ptr = raster_row;
 
     for (col = 0; col < cols; col++) {
-        n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
+        n_offset = ((size_t) row * cols + col) * Rast_cell_size(CELL_TYPE);
         if (Rast_is_null_value(index_array + n_offset, CELL_TYPE))      /* no points in cell */
             Rast_set_null_value(ptr, 1, rtype);
         else {
@@ -597,7 +598,7 @@ void write_trimmean(struct BinIndex *bin_index, void *raster_row,
     void *ptr = raster_row;
 
     for (col = 0; col < cols; col++) {
-        n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
+        n_offset = ((size_t) row * cols + col) * Rast_cell_size(CELL_TYPE);
         if (Rast_is_null_value(index_array + n_offset, CELL_TYPE))      /* no points in cell */
             Rast_set_null_value(ptr, 1, rtype);
         else {
@@ -662,32 +663,32 @@ void write_values(struct PointBinning *point_binning,
     case METHOD_N:             /* n is a straight copy */
         Rast_raster_cpy(raster_row,
                         point_binning->n_array +
-                        (row * cols * Rast_cell_size(CELL_TYPE)), cols,
+                        ((size_t) row * cols * Rast_cell_size(CELL_TYPE)), cols,
                         CELL_TYPE);
         break;
 
     case METHOD_MIN:
         Rast_raster_cpy(raster_row,
                         point_binning->min_array +
-                        (row * cols * Rast_cell_size(rtype)), cols, rtype);
+                        ((size_t) row * cols * Rast_cell_size(rtype)), cols, rtype);
         break;
 
     case METHOD_MAX:
         Rast_raster_cpy(raster_row,
                         point_binning->max_array +
-                        (row * cols * Rast_cell_size(rtype)), cols, rtype);
+                        ((size_t) row * cols * Rast_cell_size(rtype)), cols, rtype);
         break;
 
     case METHOD_SUM:
         Rast_raster_cpy(raster_row,
                         point_binning->sum_array +
-                        (row * cols * Rast_cell_size(rtype)), cols, rtype);
+                        ((size_t) row * cols * Rast_cell_size(rtype)), cols, rtype);
         break;
 
     case METHOD_RANGE:         /* (max-min) */
         ptr = raster_row;
         for (col = 0; col < cols; col++) {
-            size_t offset = (row * cols + col) * Rast_cell_size(rtype);
+            size_t offset = ((size_t) row * cols + col) * Rast_cell_size(rtype);
             double min =
                 Rast_get_d_value(point_binning->min_array + offset, rtype);
             double max =
@@ -700,8 +701,8 @@ void write_values(struct PointBinning *point_binning,
     case METHOD_MEAN:          /* (sum / n) */
         ptr = raster_row;
         for (col = 0; col < cols; col++) {
-            size_t offset = (row * cols + col) * Rast_cell_size(rtype);
-            size_t n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
+            size_t offset = ((size_t) row * cols + col) * Rast_cell_size(rtype);
+            size_t n_offset = ((size_t) row * cols + col) * Rast_cell_size(CELL_TYPE);
             int n = Rast_get_c_value(point_binning->n_array + n_offset,
                                      CELL_TYPE);
             double sum =
@@ -747,8 +748,8 @@ void write_values(struct PointBinning *point_binning,
     }
     if (point_binning->bin_coordinates) {
         for (col = 0; col < cols; col++) {
-            size_t offset = (row * cols + col) * Rast_cell_size(rtype);
-            size_t n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
+            size_t offset = ((size_t) row * cols + col) * Rast_cell_size(rtype);
+            size_t n_offset = ((size_t) row * cols + col) * Rast_cell_size(CELL_TYPE);
             int n = Rast_get_c_value(point_binning->n_array + n_offset,
                                      CELL_TYPE);
 

+ 1 - 1
raster/r.in.lidar/support.c

@@ -136,7 +136,7 @@ int row_array_get_value_row_col(void *array, int arr_row, int arr_col,
     void *ptr = array;
     ptr =
         G_incr_void_ptr(ptr,
-                        ((arr_row * cols) +
+                        (((size_t) arr_row * cols) +
                          arr_col) * Rast_cell_size(rtype));
     if (Rast_is_null_value(ptr, rtype))
         return 0;