IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 27, 2016, 11:18:36 AM (10 years ago)
Author:
eugene
Message:

trying to catch memory corruption by being extra pedantic

Location:
trunk/Ohana/src/libfits
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libfits/header/F_scan.c

    r34405 r39324  
    4444  if (p[8] != '=') return (FALSE);
    4545
     46  s = gfits_keyword_start (p); /* points at first char (not ') */
     47  q = gfits_keyword_end (p); /* points at following space or ' */
     48
     49  // these are invalid conditions:
     50  if (s - p > 80) return FALSE;
     51  if (q - p > 80) return FALSE;
     52
    4653  /* extract data into char array (exclude containing ' chars) */
     54  Nchar = MIN (80, MAX (0, (q - s)));
     55  bzero (tmp, 81);
     56  memcpy (tmp, s, Nchar);
     57
     58  // for string types, copy the data to the target pointer
    4759  if (!strcmp (mode, "%s")) {
    48     s = gfits_keyword_start (p); /* points at first char (not ') */
    49     q = gfits_keyword_end (p); /* points at following space or ' */
    50 
    51     Nchar = MAX (0, (q - s));
    52     bzero (tmp, 81);
    53     memcpy (tmp, s, Nchar);
    5460    stripwhite (tmp);
    5561    strcpy (va_arg (argp, char *), tmp);
     
    5965  /* remaining options are numerical data */
    6066  /* need to interpret 1.0d5 as 1.0e5 */
    61   s = gfits_keyword_start (p); /* points at first char (not ') */
    62   q = gfits_keyword_end (p); /* points at following space or ' */
    6367
    6468  if (!strcmp (mode, "%f") || !strcmp (mode, "%lf")) {
    65     fvalue = strtod (s, &q);
    66     if ((*q == 'd') || (*q == 'D'))
    67       fvalue *= pow (10.0, atof (q + 1));
     69    fvalue = strtod (tmp, &q);
     70    if ((*q == 'd') || (*q == 'D')) fvalue *= pow (10.0, atof (q + 1));
    6871
    6972    if (!strcmp (mode, "%f"))   { *va_arg (argp, float *)  = fvalue; return (TRUE); }
     
    7174  }
    7275
    73   value = strtoll (s, &q, 0);
     76  value = strtoll (tmp, &q, 0);
    7477  if ((*q == 'd') || (*q == 'D')) value *= pow (10.0, atof (q + 1));
    7578
     
    9699int gfits_vscan_alt (Header *header, char *field, char *mode, int N, va_list argp) {
    97100
    98   char *p, *q, *s, tmp[81];
     101  char *p, *q, *s, tmp[128];
    99102  int Nchar, status;
    100103
     
    117120  /* comment from data line */
    118121  if (!strcmp (mode, "%C")) {
    119     q = gfits_keyword_end (p) + 3;
     122    q = gfits_keyword_end (p);
     123    if (!q) return (FALSE);
     124    q += 3;
    120125    q = MIN (p + 80, q);
    121126    bzero (tmp, 81);
    122     Nchar = MIN (80, p + 80 - q);
     127    Nchar = MAX (0, MIN (80, p + 80 - q));
    123128    memcpy (tmp, q, Nchar);
    124129    stripwhite (tmp);
     
    147152int gfits_vscan_hierarch (Header *header, char *field, char *mode, int N, va_list argp) {
    148153
    149   char *p, *q, *s, tmp[81];
     154  char *p, *q, *s, tmp[128];
    150155  int Nchar, Nfield;
    151156  long long value;
     
    172177    q = MIN (p + HIERARCH_LENGTH, q);
    173178    bzero (tmp, 81);
    174     Nchar = MIN (HIERARCH_LENGTH, p + HIERARCH_LENGTH - q);
     179    Nchar = MAX (0, MIN (HIERARCH_LENGTH, p + HIERARCH_LENGTH - q));
    175180    memcpy (tmp, q, Nchar);
    176181    stripwhite (tmp);
     
    179184  }
    180185
    181   /* extract data into char array (exclude containing ' chars) */
    182   if (!strcmp (mode, "%s")) {
    183     s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
    184     q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
    185 
    186     Nchar = MIN (HIERARCH_LENGTH, MAX (0, (q - s)));
    187     bzero (tmp, 81);
    188     memcpy (tmp, s, Nchar);
    189     stripwhite (tmp);
    190     strcpy (va_arg (argp, char *), tmp);
    191     return (TRUE);
    192   }
    193  
     186  s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
     187  q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
     188
     189  // these are invalid conditions:
     190  if (s - p > 80) return FALSE;
     191  if (q - p > 80) return FALSE;
     192
    194193  /* boolean data, requires int target */
    195194  if (!strcmp (mode, "%t")) {
    196     s = gfits_hierarch_keyword_start (p, field);
    197195    if (*s == 'T') {
    198196      *va_arg (argp, int *) = TRUE;
     
    205203  }
    206204
     205  /* extract data into char array (exclude containing ' chars) */
     206  Nchar = MIN (HIERARCH_LENGTH, MAX (0, (q - s)));
     207  bzero (tmp, 81);
     208  memcpy (tmp, s, Nchar);
     209
     210  // for string types, copy the data to the target pointer
     211  if (!strcmp (mode, "%s")) {
     212    stripwhite (tmp);
     213    strcpy (va_arg (argp, char *), tmp);
     214    return (TRUE);
     215  }
     216 
    207217  /* remaining options are numerical data */
    208218  /* need to interpret 1.0d5 as 1.0e5 */
    209   s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
    210   q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
    211219
    212220  if (!strcmp (mode, "%f") || !strcmp (mode, "%lf")) {
    213     fvalue = strtod (s, &q);
    214     if ((*q == 'd') || (*q == 'D'))
    215       fvalue *= pow (10.0, atof (q + 1));
     221    fvalue = strtod (tmp, &q);
     222    if ((*q == 'd') || (*q == 'D')) fvalue *= pow (10.0, atof (q + 1));
    216223
    217224    if (!strcmp (mode, "%f"))   { *va_arg (argp, float *)  = fvalue; return (TRUE); }
     
    219226  }
    220227
    221   value = strtoll (s, &q, 0);
     228  value = strtoll (tmp, &q, 0);
    222229  if ((*q == 'd') || (*q == 'D')) value *= pow (10.0, atof (q + 1));
    223230
  • trunk/Ohana/src/libfits/table/F_set_column.c

    r38553 r39324  
    11# include <ohana.h>
    22# include <gfitsio.h>
     3# define OHANA_MEMCHECK 0
    34
    45# define SWAP_NONE
     
    3435  double Bscale, Bzero;
    3536
     37# if (OHANA_MEMCHECK)
     38  memset (tlabel,  0x7f, 256);
     39  memset (field,   0x7f, 256);
     40  memset (format,  0x7f, 256);
     41  memset (type,    0x7f, 64);
     42  memset (tmpline, 0x7f, 64);
     43
     44  ohana_memcheck_block (data);
     45# endif
     46
    3647  if (label == (char *) NULL) return (FALSE);
    3748  if (label[0] == 0) return (FALSE);
     
    4253  for (i = 1; strcasecmp (label, tlabel) && (i < Nfields + 1); i++) {
    4354    snprintf (field, 256, "TTYPE%d", i);
    44     gfits_scan (header, field, "%s", 1, tlabel);
     55    if (!gfits_scan (header, field, "%s", 1, tlabel)) return FALSE;
    4556  }
    4657  if (strcasecmp (label, tlabel)) return (FALSE);
     
    5768  }
    5869  snprintf (field, 256, "TFORM%d", N);
    59   gfits_scan (header, field, "%s", 1, format);
     70  if (!gfits_scan (header, field, "%s", 1, format)) return FALSE;
    6071
    6172  if (!gfits_bintable_format (format, type, &Nval, &Nbytes)) return (FALSE);
     
    7283  if (Ny != Nrow) return (FALSE);
    7384
     85  // if we call this function with a null pointer, we are only validating the header and allocating the data array
     86  if (!data) return TRUE;
     87
    7488  /* scan columns to find insert point */
    7589  Nstart = 0;
    7690  for (i = 1; i < N; i++) {
    7791    snprintf (field, 256, "TFORM%d", i);
    78     gfits_scan (header, field, "%s", 1, format);
     92    if (!gfits_scan (header, field, "%s", 1, format)) return FALSE;
    7993    gfits_bintable_format (format, tmpline, &Nv, &Nb);
    8094    Nstart += Nv*Nb;
     
    86100  Pin = data;
    87101  Pout = array;
     102
    88103  // does it makes sense to scale 'char' data?
    89104  if (!strcmp (type, "char")) {
     
    92107    }
    93108  }
     109
    94110  if (!strcmp (type, "byte")) {
    95111    for (i = 0; i < Nval*Nrow; i++, Pin+=Nbytes, Pout+=Nbytes) {
     
    97113    }
    98114  }
     115
    99116  if (!strcmp (type, "short")) {
    100117    for (i = 0; i < Nval*Nrow; i++, Pin+=Nbytes, Pout+=Nbytes) {
     
    105122    } 
    106123  }
     124 
    107125  if (!strcmp (type, "int")) {
    108126    for (i = 0; i < Nval*Nrow; i++, Pin+=Nbytes, Pout+=Nbytes) {
     
    113131    }
    114132  }
     133
    115134  if (!strcmp (type, "int64_t")) {
    116135    // XXX 64 bit int operations with Bzero & Bscale are inaccurate even with doubles
     
    131150    }
    132151  }
     152
    133153  if (!strcmp (type, "float")) {
    134154    for (i = 0; i < Nval*Nrow; i++, Pin+=Nbytes, Pout+=Nbytes) {
     
    139159    }
    140160  }
     161
    141162  if (!strcmp (type, "double")) {
    142163    for (i = 0; i < Nval*Nrow; i++, Pin+=Nbytes, Pout+=Nbytes) {
     
    179200  }
    180201
     202# if (OHANA_MEMCHECK)
     203  myAssert ( tlabel[255] == 0x7f, "oops");
     204  myAssert (  field[255] == 0x7f, "oops");
     205  myAssert ( format[255] == 0x7f, "oops");
     206  myAssert (   type[63]  == 0x7f, "oops");
     207  myAssert (tmpline[63]  == 0x7f, "oops");
     208
     209 ohana_memcheck (TRUE);
     210# endif
     211
    181212  free (array);
     213
    182214  return (TRUE);
    183215}
  • trunk/Ohana/src/libfits/table/F_uncompress_T.c

    r38553 r39324  
    88int gfits_distribute_table_gzp2 (FTable *table, TableField *field, char *raw, int row_start, int Nrows);
    99
     10# define OHANA_MEMCHECK 0
    1011# define VERBOSE_DUMP 0
    1112
     
    164165  gettimeofday (&startTimer, (void *) NULL);
    165166
    166   // this allocates the pointer data array for the full set of tiles (NOT the heap)
    167   char *tmpbuffer = NULL;
    168   ALLOCATE_ZERO (tmpbuffer, char, max_width*Ny);
     167  if (OHANA_MEMCHECK) ohana_memcheck (TRUE);
     168
     169  // this allocates the pointer data array for the full set of tiles (NOT the heap) by
     170  // calling with an empty data array, data values are not actually copied (this is done
     171  // below after decompression)
    169172  for (i = 0; i < Nfields; i++) {
    170     if (!gfits_set_bintable_column (tgtheader, tgttable, fields[i].ttype, tmpbuffer, Ny)) ESCAPE;
    171   }
    172   free (tmpbuffer);
     173    if (!gfits_set_bintable_column (tgtheader, tgttable, fields[i].ttype, NULL, Ny)) ESCAPE;
     174  }
    173175   
     176  if (OHANA_MEMCHECK) ohana_memcheck (TRUE);
     177
    174178  int Ntile = srcheader->Naxis[1];
    175179  int ztilelast;
Note: See TracChangeset for help on using the changeset viewer.