Example #1
0
int test_pad_check(void)
{
    unsigned char buffer[256];
    size_t t, p;

    for (t = 1; t < 32; ++t)
    {
        gen_msg(buffer, t, 32);
        ASSERT_EQ(pad_check(buffer, 32), t);
    }

    for (t = 1; t < 32; ++t)
        for (p = t; p < 32; ++p)
        {
            gen_msg(buffer, t, 32);
            buffer[p] ^= 0x01;

            ASSERT(!pad_check(buffer, 32));
        }

    gen_msg(buffer, 40, 80);

    ASSERT(!pad_check(buffer, 0));
    ASSERT(!pad_check(buffer, 32));
    ASSERT(!pad_check(buffer, 33));
    ASSERT(!pad_check(buffer, 256));
    ASSERT(!pad_check(buffer, 257));

    memset(buffer, 0xB1, 177);
    ASSERT_EQ(pad_check(buffer, 177), 177);

    return 1;
}
Example #2
0
/*
 * Strip padding from message.
 */
static int pad_decode(state_t state, uint8_t **dataptr, size_t *sizeptr)
{
    uint8_t *data = *dataptr;
    size_t size = *sizeptr;

    uint8_t pad_size;
    if (state->fixed)
    {
        pad_size = state->size;
        if (pad_size < size && !pad_check(state, data, pad_size))
        {
            return PAD_ERROR_BAD_PAD;
        }
    }
    else
    {
        uint8_t pad_size_byte = data[0];
        pad_size = state->min + pad_size_byte % (state->max - state->min + 1);
        pad_size++;
    }
    
    if (pad_size >= size)
    {
        return PAD_ERROR_BAD_LENGTH;
    }
    data += pad_size;
    size -= pad_size;
    *dataptr = data;
    *sizeptr = size;
    return 0;
}
Example #3
0
void zmalloc_check()
{
  meminfo *m, *next_m;
  char *admonishemnt;
  int total_leak = 0, num_leaks = 0, i;

  fprintf(zfd, "\n------------ Checking leaks ------------\n\n");

  for (i = 0; i < NUM_ZBUCKETS; i++) {
    for (m = memlist[i]; m; m = next_m) {
      next_m = m->next;
      if (m->addr != 0 && m->frees <= 0) {
        fprintf(zfd,"zmalloc: UNfreed memory 0x%4.4x %d bytes mallocd at %s:%d\n",
		(int)m->addr, m->size, m->file, m->line);
        if (zmalloclogging > 1) zdump(m);

        /* check padding on un-freed memory too: */
        pad_check(m);

        total_leak += m->size;
        num_leaks++;
      }
#ifndef NO_MEMORY_PADDING
      if (m->addr) free(m->addr - sizeof(beginPad));
#else
      if (m->addr) free(m->addr);
#endif
      if (m->file) free(m->file);
      free(m);
    }
  }

  if (total_leak) {
    if (total_leak > 10000)
      admonishemnt = "you must work for Microsoft.";
    else if (total_leak > 5000)
      admonishemnt = "you should be ashamed!";
    else if (total_leak > 2000)
      admonishemnt = "you call yourself a programmer?";
    else if (total_leak > 1000)
      admonishemnt = "the X consortium has a job for you...";
    else
      admonishemnt = "close, but not there yet.";
    fprintf(zfd,"zmalloc: %d leaks totalling %d bytes... %s\n",
		num_leaks, total_leak, admonishemnt);
  }
  else {
    fprintf(zfd,"zmalloc: Congratulations: leak-free code!\n");
  }

  if (zfd) {
    fflush(zfd);
    fclose(zfd);
  }
}
Example #4
0
/* doesn't actually free memory */
void zfree(unsigned char *what, char *file, int line)
{
  meminfo *m;
  int gotit = 0;

  if (!what) {
    fprintf(zfd,"zfree: ERR: Null pointer free'd: %s:%d.\n", file, line);
    return;
  }

  /* look up allocated mem in list: */
  for (m = memlist[GET_ZBUCKET(what)]; m; m = m->next) {
    if (m->addr == what) {
      /* got it.  Print it if verbose: */
      if (zmalloclogging > 2) {
	fprintf(zfd,"zfree: Freed 0x%4.4x %d bytes mallocd at %s:%d, freed at %s:%d\n",
		    (int)m->addr, m->size, m->file, m->line, file, line);
      }
      /* check the padding: */
      pad_check(m);

      /* note that we freed the memory */
      m->frees++;  

      /* check to see if it was freed > once */
      if (m->frees > 1) {
        fprintf(zfd,"zfree: ERR: multiple frees! 0x%4.4x %d bytes\n"
		    "       mallocd at %s:%d, freed at %s:%d.\n",
                    (int)m->addr, m->size, m->file, m->line, file, line);
	if (zmalloclogging > 1) zdump(m);
      }
      gotit++;
    }
  } /* for.. */

  if (!gotit) {
    fprintf(zfd,"zfree: ERR: attempt to free unallocated memory 0x%4.4x at %s:%d.\n",
		(int)what, file, line);
  }
  if (gotit > 1) {
    /* this shouldn't happen, eh? */
    fprintf(zfd,"zfree: ERR: Multiply-allocd memory 0x%4.4x.\n", (int)what);
  }
}