IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

trying to catch memory corruption by being extra pedantic

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.