IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39324 for trunk


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
Files:
6 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;
  • trunk/Ohana/src/libohana/Makefile

    r39242 r39324  
    6666$(DESTLIB)/libohana.$(DLLTYPE): $(LIB)/libohana.$(ARCH).$(DLLTYPE)
    6767
    68 TESTPROG = sprintf_floats
    69 # TESTPROG = memtest typetest string
     68# TESTPROG = sprintf_floats
     69TESTPROG = memtest typetest string
    7070
    7171$(TESTPROG) : % : $(TESTBIN)/% $(OBJS)
  • trunk/Ohana/src/libohana/include/ohana_allocate.h

    r38986 r39324  
    4141void  ohana_free (const char *file, int line, const char *func, void *in);
    4242void  ohana_memdump_func (int mode);
    43 int   ohana_memcheck_func (int mode);
     43int   ohana_memcheck_func (const char *myFile, int myLine, const char *myFunc, int mode);
     44void ohana_memcheck_block_func (const char *myFile, int myLine, const char *myFunc, void *in);
    4445void  real_free (void *in);
    4546
    46 # define ohana_memcheck(X) ohana_memcheck_func (X)
     47# define ohana_memcheck(X) ohana_memcheck_func (__FILE__, __LINE__, __func__, X)
     48# define ohana_memcheck_block(X) ohana_memcheck_block_func (__FILE__, __LINE__, __func__, X)
    4749# define ohana_memdump(X) ohana_memdump_func (X);
    4850
     
    7779
    7880# define ohana_memcheck(X) ohana_memcheck_noop (X)
     81# define ohana_memcheck_block(X) ohana_memcheck_noop (X)
    7982# define ohana_memdump(X) /* NOP */
    8083void  real_free (void *in);
  • trunk/Ohana/src/libohana/src/ohana_allocate.c

    r38986 r39324  
    1818
    1919static OhanaMemblock *lastBlock = NULL;
     20static int Nblock = 0;
    2021
    2122# if (TEST_SAVE_FREE_BLOCKS)
     
    9596  lastBlock = new;
    9697
     98  Nblock ++;
    9799  pthread_mutex_unlock(&memBlockListMutex);
    98100
     
    212214  if (!in) return;
    213215
     216  if (!lastBlock) ohana_memabort ("no memory allocated yet (%s@%d : %s)\n", file, line, func);
     217
    214218  // fprintf (stderr, "free %zx\n", (size_t) in);
    215219
     
    221225  // fprintf (stderr, "  file: %s, line: %d, func: %s, size: %zd, addr: %zx\n",
    222226  // ref->file, ref->line, ref->func, ref->size, (size_t) ref);
     227
     228  if (!lastBlock) ohana_memabort ("corruption? (%s@%d : %s)\n", file, line, func);
    223229
    224230  pthread_mutex_lock(&memBlockListMutex);
     
    227233  OhanaMemblock *prevBlock = ref->prevBlock;
    228234
     235  if (!nextBlock && !prevBlock && (Nblock > 1)) ohana_memabort ("orphan block?? (%s@%d : %s)\n", file, line, func);
     236
    229237  // remove this memBlock from the list
    230238  if (nextBlock) {
     
    238246  }
    239247 
     248  if (!lastBlock && (Nblock > 1)) ohana_memabort ("corruption? (%s@%d : %s)\n", file, line, func);
     249
    240250  // XXX FOR TESTING, save the freed blocks
    241251# if (TEST_SAVE_FREE_BLOCKS)
     
    252262  memset (ptr, 0x77, ref->size);
    253263
     264  Nblock --;
     265  if (Nblock < 0) ohana_memabort ("excess frees? (%s@%d : %s)\n", file, line, func);
     266
    254267  pthread_mutex_unlock(&memBlockListMutex);
    255268 
     
    265278}
    266279
    267 int ohana_memcheck_func (int VERBOSE) {
     280int ohana_memcheck_func (const char *myFile, int myLine, const char *myFunc, int VERBOSE) {
    268281
    269282  if (!lastBlock) {
     
    310323
    311324  if (Ntotal || VERBOSE) {
    312     fprintf (stderr, "%zd memory blocks allocated (%zd bytes total), %zd good, %zd bad\n", Ntotal, Nbytes, Ngood, Nbad);
     325    fprintf (stderr, "%zd memory blocks allocated (%zd bytes total), %zd good, %zd bad ", Ntotal, Nbytes, Ngood, Nbad);
     326    fprintf (stderr, "@ %s:%d (%s)\n", myFile, myLine, myFunc);
    313327  }
    314328
     
    415429
    416430  if (Ntotal_drop || VERBOSE) {
    417     fprintf (stderr, "%zd memory blocks dropped   (%zd bytes total), %zd good, %zd bad\n", Ntotal_drop, Nbytes_drop, Ngood_drop, Nbad_drop);
     431    fprintf (stderr, "%zd memory blocks dropped   (%zd bytes total), %zd good, %zd bad", Ntotal_drop, Nbytes_drop, Ngood_drop, Nbad_drop);
     432    fprintf (stderr, "@ %s:%d (%d)\n", myFile, myLine, myFunc);
    418433  }
    419434
     
    422437 
    423438  return status;
     439}
     440
     441void ohana_memcheck_block_func (const char *myFile, int myLine, const char *myFunc, void *in) {
     442
     443  OhanaMemblock *ref;
     444
     445  if (!in) return;
     446
     447  if (!lastBlock) ohana_memabort ("no memory allocated yet (%s@%d : %s)\n", myFile, myLine, myFunc);
     448
     449  ref = (OhanaMemblock *) in - 1;
     450
     451  if (ref->freed) ohana_memabort ("memory already freed (%s, %d, %s [%d bytes])\n", ref->file, ref->line, ref->func, ref->size);
     452
     453  OhanaMemblock *nextBlock = ref->nextBlock;
     454  OhanaMemblock *prevBlock = ref->prevBlock;
     455
     456  if (!nextBlock && !prevBlock) ohana_memabort ("orphan block?? (%s@%d : %s)\n", myFile, myLine, myFunc);
    424457}
    425458
Note: See TracChangeset for help on using the changeset viewer.