[GRASS5] PATCH: alloc_cell.c

Any objections to me applying this patch to keep consistent with size_t
*alloc() types?

Index: lib/gis/alloc_cell.c

RCS file: /grassrepository/grass6/lib/gis/alloc_cell.c,v
retrieving revision 2.0
diff -u -r2.0 alloc_cell.c
--- lib/gis/alloc_cell.c 9 Nov 2004 12:13:47 -0000 2.0
+++ lib/gis/alloc_cell.c 17 Oct 2005 05:14:16 -0000
@@ -16,11 +16,10 @@
  * If <em>data_type</em> is DCELL_TYPE, returns sizeof(DCELL)
  *
  * \param data_type
- * \return int
+ * \return size_t
  */

-int G_raster_size (data_type)
- RASTER_MAP_TYPE data_type;
+size_t G_raster_size (RASTER_MAP_TYPE data_type)
{
     return (type_size[F2I(data_type)]);
}
Index: include/gisdefs.h

RCS file: /grassrepository/grass6/include/gisdefs.h,v
retrieving revision 1.37
diff -u -r1.37 gisdefs.h
--- include/gisdefs.h 15 Aug 2005 09:56:07 -0000 1.37
+++ include/gisdefs.h 17 Oct 2005 05:14:44 -0000
@@ -45,7 +45,7 @@
void G_free(void *);

/* alloc_cell.c */
-int G_raster_size(RASTER_MAP_TYPE);
+size_t G_raster_size(RASTER_MAP_TYPE);
CELL *G_allocate_cell_buf(void);
void *G_allocate_raster_buf(RASTER_MAP_TYPE);
CELL *G_allocate_c_raster_buf(void);

--
Brad Douglas <rez@touchofmadness.com>

Brad Douglas wrote:

Any objections to me applying this patch to keep consistent with size_t
*alloc() types?

Have you verified that nothing which calls G_raster_size() is relying
upon the return value being signed?

The issue is that if either operand to an arithmetic operation is
unsigned, the result is unsigned. This could be a problem if something
is multiplying a potentially-negative index by the result of
G_raster_size(), particularly if the result is promoted (explicitly or
implicitly) to a long on platforms where long is 64 bits:

  $ cat foo.c

  #include <stdio.h>
  
  int main(void)
  {
    int x = -5;
    int k1 = 2;
    unsigned int k2 = 2;
    /* my longs are only 32 bits, so use long long instead */
    long long y1 = x * k1;
    long long y2 = x * k2;
  
    printf("y1 = %lld\ny2 = %lld\n", y1, y2);
  
    return 0;
  }
  
  $ cc foo.c
  $ ./a.out
  y1 = -10
  y2 = 4294967286

--
Glynn Clements <glynn@gclements.plus.com>