inline APTR StkPop(struct Stack *Stack) { APTR Retval = NULL; if(Stack && (Stack->Used > 0)) { Retval = Stack->Data[--Stack->Used]; #ifdef NO_DIRTY_TRICKS { APTR *NewBuf; if(Stack->Used < (Stack->Size/2)) { ULONG NewSize; NewSize = Stack->Size - MINPUDDLE; NewSize = max(NewSize, MINPUDDLE); if(NewBuf = saferealloc(Stack->Data, (size_t) NewSize * sizeof(APTR))) { Stack->Size = NewSize; Stack->Data = NewBuf; } } } #endif } return(Retval); }
int StkPush(void *Data, struct Stack *Stack) { unsigned long NewSize; void **NewBuf; if (Data && Stack) { if (Stack->Used >= Stack->Size) { NewSize = Stack->Size + MINPUDDLE; if ((NewBuf = saferealloc(Stack->Data, (size_t) NewSize * sizeof(void *)))) { Stack->Size = NewSize; Stack->Data = NewBuf; } else return (FALSE); } Stack->Data[Stack->Used++] = Data; return (TRUE); } return (FALSE); }
/* SaveError() takes printf style args and saves the result in LastError */ static void SaveError(pTHXo_ char* pat, ...) { va_list args; SV *msv; char *message; STRLEN len; /* This code is based on croak/warn, see mess() in util.c */ va_start(args, pat); msv = vmess(pat, &args); va_end(args); message = SvPV(msv,len); len++; /* include terminating null char */ /* Allocate some memory for the error message */ if (LastError) LastError = (char*)saferealloc(LastError, len) ; else LastError = (char *) safemalloc(len) ; /* Copy message into LastError (including terminating null char) */ strncpy(LastError, message, len) ; DLDEBUG(2,PerlIO_printf(Perl_debug_log, "DynaLoader: stored error msg '%s'\n",LastError)); }
BOOL StkPush(const APTR Data, struct Stack *Stack) { ULONG NewSize; APTR *NewBuf; if(Data && Stack) { if(Stack->Used >= Stack->Size) { NewSize = Stack->Size + MINPUDDLE; if((NewBuf = saferealloc(Stack->Data, (size_t) NewSize * sizeof(APTR)))) { Stack->Size = NewSize; Stack->Data = NewBuf; } else return(FALSE); } Stack->Data[Stack->Used++] = Data; return(TRUE); } return(FALSE); }
size_t mm_mem_io_c::_write(const void *buffer, size_t size) { if (m_read_only) throw mtx::mm_io::wrong_read_write_access_x(); int64_t wbytes; if ((m_pos + size) >= m_allocated) { if (m_increase) { int64_t new_allocated = m_pos + size - m_allocated; new_allocated = ((new_allocated / m_increase) + 1 ) * m_increase; m_allocated += new_allocated; m_mem = (unsigned char *)saferealloc(m_mem, m_allocated); wbytes = size; } else wbytes = m_allocated - m_pos; } else wbytes = size; if ((m_pos + size) > m_mem_size) m_mem_size = m_pos + size; memcpy(&m_mem[m_pos], buffer, wbytes); m_pos += wbytes; m_cached_size = -1; return wbytes; }
/***************************************************************************** * AllocColorset() grows the size of the Colorset array to include set n * Colorset_struct *Colorset will be altered * returns the address of the member *****************************************************************************/ void AllocColorset(int n) { /* do nothing if it already exists */ if (n < nColorsets) return; /* increment n to get the required array size, get a new array */ Colorset = (colorset_struct *)saferealloc((char *)Colorset, ++n * sizeof(colorset_struct)); /* zero out the new members */ memset(&Colorset[nColorsets], 0, (n - nColorsets) * sizeof(colorset_struct)); /* copy colorset 0 pixels into new members so that if undefined ones are * referenced at least they don't give black on black */ if (n > 1) { for ( ; nColorsets < n; nColorsets++) { Colorset[nColorsets].fg = Colorset[0].fg; Colorset[nColorsets].bg = Colorset[0].bg; Colorset[nColorsets].hilite = Colorset[0].hilite; Colorset[nColorsets].shadow = Colorset[0].shadow; } } nColorsets = n; }
void UpdateString(char **string, const char *value) { if (value==NULL) return; *string = saferealloc(*string, strlen(value)+1); strcpy(*string, value); }
/** * Remove a channel from its associated port mapping structure * * Take the channel given and remove it from its parent port_map structure, * closing it first and removing it from the fd_map in the gateway struct that * maps client sockets -> channels. * * All channel pointers in pm->ch[] higher than this one are shifted down by * one position and the array is shrunk appropriately. * * @cs channel structure to remove * @return Nothing, if there are errors here they are either logged or the * program exits (on malloc failure) */ void remove_channel_from_map(struct chan_sock *cs) { struct static_port_map *pm = cs->parent; int i; if(cs->parent == NULL) log_exit(FATAL_ERROR, "Corrupt chan_sock parent %p->parent NULL", cs); for(i = 0; i < pm->n_channels; i++) { if(pm->ch[i] == cs) { if( ssh_channel_is_open(cs->channel) && ssh_channel_close(cs->channel) != SSH_OK) log_msg("Error on channel close for %s", pm->parent->name); ssh_channel_free(cs->channel); break; } } debug("Destroy channel %p, closing fd=%d", cs->channel, cs->sock_fd); for(; i < pm->n_channels - 1; i++) pm->ch[i] = pm->ch[i + 1]; saferealloc((void **)&pm->ch, pm->n_channels * sizeof(cs), "pm->channel realloc"); pm->n_channels -= 1; close(cs->sock_fd); /* Remove this fd from parent gw's fd_map */ remove_fdmap(pm->parent->chan_sock_fdmap, cs->sock_fd); free(cs); }
/* * AllocColorset() grows the size of the Colorset array to include set n * colorset_t *Colorset will be altered * returns the address of the member */ void AllocColorset(int n) { /* do nothing if it already exists */ if (n < nColorsets) { return; } /* increment n to get the required array size, get a new array */ Colorset = (colorset_t *)saferealloc( (char *)Colorset, ++n * sizeof(colorset_t)); /* zero out colorset 0 it's always defined so will be filled in during module startup */ if (n == 0) { memset( &Colorset[nColorsets], 0, (n - nColorsets) * sizeof(colorset_t)); } else { /* copy colorset 0 into new members so that if undefined ones * are referenced at least they don't give black on black */ for ( ; nColorsets < n; nColorsets++) { memcpy( &Colorset[nColorsets], Colorset, sizeof(colorset_t)); } } nColorsets = n; return; }
void *StkPop(struct Stack *Stack) { void *Retval = NULL; if (Stack && (Stack->Used > 0)) { Retval = Stack->Data[--Stack->Used]; #ifdef NO_DIRTY_TRICKS { void **NewBuf; if (Stack->Used < (Stack->Size / 2)) { unsigned long NewSize; NewSize = Stack->Size - MINPUDDLE; NewSize = max(NewSize, MINPUDDLE); if (NewBuf = saferealloc(Stack->Data, (size_t) NewSize * sizeof(void *))) { Stack->Size = NewSize; Stack->Data = NewBuf; } } } #endif } return (Retval); }
/* Example: filter allow localhost "/etc/tinyhost/filter" */ int add_new_filter(char *aclname, char *expression) { static int filter_count = 1; /* First, add space for another pointer to the filter array. */ config.filters = saferealloc(config.filters, sizeof(struct filter_s *) * (filter_count + 1)); if (!config.filters) return (-1); /* Allocate space for an actual structure */ config.filters[filter_count - 1] = safemalloc(sizeof(struct filter_s)); if (!config.filters[filter_count - 1]) return (-1); log_message(LOG_INFO, "%s: New filter '%s' for %s", __func__, expression, aclname); /* Set values for filters structure. */ config.filters[filter_count - 1]->expression = safestrdup(expression); if (!config.filters[filter_count - 1]->expression) return (-1); config.filters[filter_count - 1]->aclname = safestrdup(aclname); if (!config.filters[filter_count - 1]->aclname) return (-1); /* Set NULL to denote end of array */ config.filters[filter_count] = NULL; filter_count++; return (0); }
static void process_regular_char_input(unsigned char *buf) { char *sp, *dp, *ep; /* n is the space actually used. buf is the size of the buffer size is the size of the field on the screen size is used as the increment, arbitrarily. */ if (++(CF.cur_input->input.n) >= CF.cur_input->input.buf) { CF.cur_input->input.buf += CF.cur_input->input.size; CF.cur_input->input.value = (char *)saferealloc(CF.cur_input->input.value, CF.cur_input->input.buf); } dp = CF.cur_input->input.value + CF.cur_input->input.n; sp = dp - 1; ep = CF.cur_input->input.value + CF.rel_cursor; for (; *dp = *sp, sp != ep; sp--, dp--); *ep = buf[0]; CF.rel_cursor++; CF.abs_cursor++; if (CF.abs_cursor >= CF.cur_input->input.size) { if (CF.rel_cursor < CF.cur_input->input.n) CF.abs_cursor = CF.cur_input->input.size - 1; else CF.abs_cursor = CF.cur_input->input.size; CF.cur_input->input.left = CF.rel_cursor - CF.abs_cursor; } }
void growstr(char **strptr, int *curlen, int newlen) { if (newlen > *curlen) { /* need more room? */ if (*curlen) *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen); else *strptr = (char *) safemalloc((MEM_SIZE)newlen); *curlen = newlen; } }
void *safegrowarray(void *ptr, size_t *allocated, size_t eltsize, size_t oldlen, size_t extralen, bool secret) { /* The largest value we can safely multiply by eltsize */ assert(eltsize > 0); size_t maxsize = (~(size_t)0) / eltsize; size_t oldsize = *allocated; /* Range-check the input values */ assert(oldsize <= maxsize); assert(oldlen <= maxsize); assert(extralen <= maxsize - oldlen); /* If the size is already enough, don't bother doing anything! */ if (oldsize > oldlen + extralen) return ptr; /* Find out how much we need to grow the array by. */ size_t increment = (oldlen + extralen) - oldsize; /* Invent a new size. We want to grow the array by at least * 'increment' elements; by at least a fixed number of bytes (to * get things started when sizes are small); and by some constant * factor of its old size (to avoid repeated calls to this * function taking quadratic time overall). */ if (increment < 256 / eltsize) increment = 256 / eltsize; if (increment < oldsize / 16) increment = oldsize / 16; /* But we also can't grow beyond maxsize. */ size_t maxincr = maxsize - oldsize; if (increment > maxincr) increment = maxincr; size_t newsize = oldsize + increment; void *toret; if (secret) { toret = safemalloc(newsize, eltsize); memcpy(toret, ptr, oldsize * eltsize); smemclr(ptr, oldsize * eltsize); sfree(ptr); } else { toret = saferealloc(ptr, newsize, eltsize); } *allocated = newsize; return toret; }
/* * Build a URL from parts. */ static int build_url (char **url, const char *host, int port, const char *path) { int len; assert (url != NULL); assert (host != NULL); assert (port > 0 && port < 32768); assert (path != NULL); len = strlen (host) + strlen (path) + 14; *url = (char *) saferealloc (*url, len); if (*url == NULL) return -1; return snprintf (*url, len, "http://%s:%d%s", host, port, path); }
/** * Add a new channel to a specific remote-host mapping * * Creates a new channel in the mapping and returns a pointer to it, updating * the fd_map in the gateway (pm->parent) that maps connection socket fd to * the new channel structure. * * @pm mapping to add a new channel to * @channel newly created ssh_channel * @sock_fd socket that the client is connected on * @return Pointer to newly created channel struct */ struct chan_sock * add_channel_to_map(struct static_port_map *pm, ssh_channel channel, int sock_fd) { struct chan_sock *cs = safemalloc(sizeof(struct chan_sock), "add ch cs"); debug("Adding channel %p to map %s:%d", channel, pm->remote_host, pm->remote_port); saferealloc((void **)&pm->ch, (pm->n_channels + 1) * sizeof(pm->ch), "pm->channel realloc"); cs->channel = channel; cs->sock_fd = sock_fd; add_fdmap(pm->parent->chan_sock_fdmap, sock_fd, cs); pm->ch[pm->n_channels] = cs; pm->n_channels++; cs->parent = pm; return cs; }
/* * Send a "message" to the file descriptor provided. This handles the * differences between the various implementations of vsnprintf. This code * was basically stolen from the snprintf() man page of Debian Linux * (although I did fix a memory leak. :) */ int write_message(int fd, const char *fmt, ...) { ssize_t n; size_t size = (1024 * 8); /* start with 8 KB and go from there */ char *buf, *tmpbuf; va_list ap; if ((buf = safemalloc(size)) == NULL) return -1; while (1) { va_start(ap, fmt); n = vsnprintf(buf, size, fmt, ap); va_end(ap); /* If that worked, break out so we can send the buffer */ if (n > -1 && n < size) break; /* Else, try again with more space */ if (n > -1) /* precisely what is needed (glibc2.1) */ size = n + 1; else /* twice the old size (glibc2.0) */ size *= 2; if ((tmpbuf = saferealloc(buf, size)) == NULL) { safefree(buf); return -1; } else buf = tmpbuf; } if (safe_write(fd, buf, n) < 0) { safefree(buf); return -1; } safefree(buf); return 0; }
void memory_c::resize(size_t new_size) throw() { if (!its_counter) its_counter = new counter(nullptr, 0, false); if (its_counter->is_free) { its_counter->ptr = (unsigned char *)saferealloc(its_counter->ptr, new_size + its_counter->offset); its_counter->size = new_size + its_counter->offset; } else { auto tmp = (unsigned char *)safemalloc(new_size); memcpy(tmp, its_counter->ptr + its_counter->offset, std::min(new_size, its_counter->size - its_counter->offset)); its_counter->ptr = tmp; its_counter->is_free = true; its_counter->size = new_size; } }
STR * str_mortal(STR *oldstr) { register STR *str = str_new(0); static long tmps_size = -1; str_sset(str,oldstr); if (++tmps_max > tmps_size) { tmps_size = tmps_max; if (!(tmps_size & 127)) { if (tmps_size) tmps_list = (STR**)saferealloc((char*)tmps_list, (tmps_size + 128) * sizeof(STR*) ); else tmps_list = (STR**)safemalloc(128 * sizeof(char*)); } } tmps_list[tmps_max] = str; return str; }
/** * Add a mapping (local port -> remote host + port) to the gateway structure. * * Creates a listening port for the local side and adds the fd to the fd_map * on the gateway that maps listening ports to the map structure. * * The mappings are stored in an array of pointers gw->pm that is grown * appropriately and gw->n_maps stores the size of this array. * * @gw gateway structure to add to * @local_port the local port to listen on -- bound to localhost:NNNN * @host the remote host to tunnel to * @remote_port the port on the remote side to connect to * @return Nothing, if anything fails here the program exits */ void add_map_to_gw(struct gw_host *gw, uint32_t local_port, char *host, uint32_t remote_port) { struct static_port_map *spm; debug("Adding map %d %s:%d to %s", local_port, host, remote_port, gw->name); spm = safemalloc(sizeof(struct static_port_map), "static_port_map alloc"); spm->parent = gw; spm->local_port = local_port; spm->remote_host = safestrdup(host, "spm strdup hostname"); spm->remote_port = remote_port; spm->ch = safemalloc(sizeof(struct chan_sock *), "spm->ch"); spm->listen_fd = create_listen_socket(local_port, "localhost"); add_fdmap(gw->listen_fdmap, spm->listen_fd, spm); spm->parent = gw; spm->n_channels = 0; saferealloc((void **)&gw->pm, (gw->n_maps + 1) * sizeof(spm), "gw->pm realloc"); gw->pm[gw->n_maps++] = spm; }
void hsplit(HASH *tb) { const int oldsize = tb->tbl_max + 1; int newsize = oldsize * 2; int i; HENT **a; HENT **b; HENT *entry; HENT **oentry; a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*)); memset(&a[oldsize], 0, oldsize * sizeof(HENT*)); /* zero second half */ tb->tbl_max = --newsize; tb->tbl_array = a; for (i=0; i<oldsize; i++,a++) { if (!*a) /* non-existent */ continue; b = a+oldsize; for (oentry = a, entry = *a; entry; entry = *oentry) { if ((entry->hent_hash & newsize) != i) { *oentry = entry->hent_next; entry->hent_next = *b; if (!*b) tb->tbl_fill++; *b = entry; continue; } else oentry = &entry->hent_next; } if (!*a) /* everything moved */ tb->tbl_fill--; } }
static void term_send(char *buf, int len) { while (len--) { char c; c = *buf++; switch (term_quotenext ? ' ' : c) { /* * ^h/^?: delete one char and output one BSB * ^w: delete, and output BSBs, to return to last space/nonspace * boundary * ^u: delete, and output BSBs, to return to BOL * ^c: Do a ^u then send a telnet IP * ^z: Do a ^u then send a telnet SUSP * ^\: Do a ^u then send a telnet ABORT * ^r: echo "^R\n" and redraw line * ^v: quote next char * ^d: if at BOL, end of file and close connection, else send line * and reset to BOL * ^m: send line-plus-\r\n and reset to BOL */ case CTRL('H'): case CTRL('?'): /* backspace/delete */ if (term_buflen > 0) { bsb(plen(term_buf[term_buflen-1])); term_buflen--; } break; case CTRL('W'): /* delete word */ while (term_buflen > 0) { bsb(plen(term_buf[term_buflen-1])); term_buflen--; if (term_buflen > 0 && isspace(term_buf[term_buflen-1]) && !isspace(term_buf[term_buflen])) break; } break; case CTRL('U'): /* delete line */ case CTRL('C'): /* Send IP */ case CTRL('\\'): /* Quit */ case CTRL('Z'): /* Suspend */ while (term_buflen > 0) { bsb(plen(term_buf[term_buflen-1])); term_buflen--; } back->special (TS_EL); if( c == CTRL('C') ) back->special (TS_IP); if( c == CTRL('Z') ) back->special (TS_SUSP); if( c == CTRL('\\') ) back->special (TS_ABORT); break; case CTRL('R'): /* redraw line */ c_write("^R\r\n", 4); { int i; for (i = 0; i < term_buflen; i++) pwrite(term_buf[i]); } break; case CTRL('V'): /* quote next char */ term_quotenext = TRUE; break; case CTRL('D'): /* logout or send */ if (term_buflen == 0) { back->special (TS_EOF); } else { back->send(term_buf, term_buflen); term_buflen = 0; } break; case CTRL('M'): /* send with newline */ if (term_buflen > 0) back->send(term_buf, term_buflen); if (cfg.protocol == PROT_RAW) back->send("\r\n", 2); else back->send("\r", 1); c_write("\r\n", 2); term_buflen = 0; break; default: /* get to this label from ^V handler */ if (term_buflen >= term_bufsiz) { term_bufsiz = term_buflen + 256; term_buf = saferealloc(term_buf, term_bufsiz); } term_buf[term_buflen++] = c; pwrite(c); term_quotenext = FALSE; break; } } }
void * countrealloc (const char *fname, int line, void *ptr, size_t length) { if (ptr != NULL && length == 0) countfree (fname, line, ptr); if (length == 0) return NULL; if (ptr != NULL) { mem *m = NULL; ASHashResult res ; if( allocs_hash != NULL ) { ASHashData hd ; service_mode++ ; if( remove_hash_item (allocs_hash, AS_HASHABLE(ptr), &hd.vptr, False) == ASH_Success ) { m = hd.vptr ; if( (m->type & 0xff) != C_MEM ) { show_error( "while deallocating pointer 0x%lX discovered that it was allocated with different type", ptr ); print_simple_backtrace(); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif m = NULL ; } } service_mode-- ; } if (m == NULL) { show_error ("countrealloc:attempt in %s:%d to realloc memory(%p) that was never allocated!\n", fname, line, ptr); print_simple_backtrace(); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif return NULL; } if ((m->type & 0xff) == C_MEM) { total_alloc -= m->length; total_alloc += length; if (total_alloc > max_alloc) max_alloc = total_alloc; } else { total_x_alloc -= m->length; total_x_alloc += length; if (total_x_alloc > max_x_alloc) max_x_alloc = total_x_alloc; } m->fname = fname; m->line = line; m->length = length; m->type = C_MEM | C_REALLOC; m->ptr = saferealloc (ptr, length); m->freed = 0; ptr = m->ptr; if( (res = add_hash_item( allocs_hash, (ASHashableValue)ptr, m )) != ASH_Success ) { show_error( "failed to log allocation for pointer 0x%lX - result = %d", ptr, res); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif } reallocations++; } else ptr = countmalloc (fname, line, length); return ptr; }
/* * alloc_colorset() grows the size of the Colorset array to include set n * colorset_t *Colorset will be altered * returns the address of the member */ void alloc_colorset(int n) { /* do nothing if it already exists */ if (n < nColorsets) { return; } else { Colorset = (colorset_t *)saferealloc( (char *)Colorset, (n + 1) * sizeof(colorset_t)); memset( &Colorset[nColorsets], 0, (n + 1 - nColorsets) * sizeof(colorset_t)); } if (n == 0) { update_root_pixmap(0); } /* initialize new colorsets to black on gray */ while (nColorsets <= n) { colorset_t *ncs = &Colorset[nColorsets]; if (PictureUseBWOnly()) { char g_bits[] = {0x0a, 0x05, 0x0a, 0x05, 0x08, 0x02, 0x08, 0x02, 0x01, 0x02, 0x04, 0x08}; /* monochrome monitors get black on white */ /* with a gray pixmap background */ ncs->fg = GetColor(black); ncs->bg = GetColor(white); ncs->hilite = GetColor(white); ncs->shadow = GetColor(black); ncs->fgsh = GetColor(white); ncs->tint = GetColor(black); ncs->icon_tint = GetColor(black); ncs->pixmap = XCreatePixmapFromBitmapData( dpy, Scr.NoFocusWin, &g_bits[4 * (nColorsets % 3)], 4, 4, PictureBlackPixel(), PictureWhitePixel(), Pdepth); ncs->width = 4; ncs->height = 4; } else { ncs->fg = GetColor(black); ncs->bg = GetColor(gray); ncs->hilite = GetHilite(ncs->bg); ncs->shadow = GetShadow(ncs->bg); ncs->fgsh = GetForeShadow(ncs->fg, ncs->bg); ncs->tint = GetColor(black); ncs->icon_tint = GetColor(black); } ncs->fg_tint = ncs->bg_tint = GetColor(black); /* set flags for fg contrast, bg average */ /* in case just a pixmap is given */ ncs->color_flags = FG_CONTRAST | BG_AVERAGE; ncs->fg_saved = ncs->fg; ncs->fg_alpha_percent = 100; ncs->image_alpha_percent = 100; ncs->icon_alpha_percent = 100; ncs->tint_percent = 0; ncs->icon_tint_percent = 0; ncs->fg_tint_percent = ncs->bg_tint_percent = 0; ncs->dither = (PictureDitherByDefault())? True:False; nColorsets++; } }