int mfi_config_read_opcode(int fd, uint32_t opcode, struct mfi_config_data **configp, uint8_t *mbox, size_t mboxlen) { struct mfi_config_data *config; uint32_t config_size; int error; /* * Keep fetching the config in a loop until we have a large enough * buffer to hold the entire configuration. */ config = NULL; config_size = 1024; fetch: config = reallocf(config, config_size); if (config == NULL) return (-1); if (mfi_dcmd_command(fd, opcode, config, config_size, mbox, mboxlen, NULL) < 0) { error = errno; free(config); errno = error; return (-1); } if (config->size > config_size) { config_size = config->size; goto fetch; } *configp = config; return (0); }
static int merge_alias(const char *name, build_hostent_t *h) { int i; if (name == NULL) return 0; if (h == NULL) return 0; if ((h->host.h_name != NULL) && (string_equal(name, h->host.h_name))) return 0; for (i = 0; i < h->alias_count; i++) { if (string_equal(name, h->host.h_aliases[i])) return 0; } if (h->alias_count == 0) h->host.h_aliases = (char **)calloc(2, sizeof(char *)); else h->host.h_aliases = (char **)reallocf(h->host.h_aliases, (h->alias_count + 2) * sizeof(char *)); if (h->host.h_aliases == NULL) { h->alias_count = 0; return -1; } h->host.h_aliases[h->alias_count] = lower_case(name); h->alias_count++; h->host.h_aliases[h->alias_count] = NULL; return 0; }
int mfi_pd_get_list(int fd, struct mfi_pd_list **listp, uint8_t *statusp) { struct mfi_pd_list *list; uint32_t list_size; /* * Keep fetching the list in a loop until we have a large enough * buffer to hold the entire list. */ list = NULL; list_size = 1024; fetch: list = reallocf(list, list_size); if (list == NULL) return (-1); if (mfi_dcmd_command(fd, MFI_DCMD_PD_GET_LIST, list, list_size, NULL, 0, statusp) < 0) { free(list); return (-1); } if (list->size > list_size) { list_size = list->size; goto fetch; } *listp = list; return (0); }
u_char * __collate_substitute(const u_char *s) { #if 0 int dest_len, len, nlen; int delta = strlen(s); u_char *dest_str = NULL; if (s == NULL || *s == '\0') return (__collate_strdup("")); delta += delta / 8; dest_str = malloc(dest_len = delta); if (dest_str == NULL) __collate_err(EX_OSERR, __func__); len = 0; while (*s) { nlen = len + strlen(__collate_substitute_table[*s]); if (dest_len <= nlen) { dest_str = reallocf(dest_str, dest_len = nlen + delta); if (dest_str == NULL) __collate_err(EX_OSERR, __func__); } (void)strcpy(dest_str + len, __collate_substitute_table[*s++]); len = nlen; } return (dest_str); #endif printf("Warning: __collate_substitute() stubbed out!\n"); return NULL; }
int append_path_segment(char** path, const char* segment) { if (*path == NULL || segment == NULL) return -1; size_t pathlen = strlen(*path); size_t seglen = strlen(segment); if (seglen == 0) return 0; // Does the segment already exist in the path? // (^|:)segment(:|$) char* match = strstr(*path, segment); while (match) { if ((match == *path || match[-1] == ':') && (match[seglen] == ':' || match[seglen] == 0)) { return 0; } match = strstr(match+1, segment); } // size = pathlen + ':' + segment + '\0' size_t size = pathlen + seglen + 2; *path = reallocf(*path, size); if (*path == NULL) return -1; if (pathlen > 0) strlcat(*path, ":", size); strlcat(*path, segment, size); return 0; }
int mfi_config_read(int fd, struct mfi_config_data **configp) { struct mfi_config_data *config; uint32_t config_size; int error; /* * Keep fetching the config in a loop until we have a large enough * buffer to hold the entire configuration. */ config = NULL; config_size = 1024; fetch: config = reallocf(config, config_size); if (config == NULL) return (-1); if (mfi_dcmd_command(fd, MFI_DCMD_CFG_READ, config, config_size, NULL, 0, NULL) < 0) { error = errno; free(config); errno = error; return (-1); } if (config->size > config_size) { config_size = config->size; goto fetch; } *configp = config; return (0); }
/* * brace_subst -- * Replace occurrences of {} in s1 with s2 and return the result string. */ void brace_subst(char *orig, char **store, char *path, size_t len) { const char *pastorigend, *p, *q; char *dst; size_t newlen, plen; plen = strlen(path); newlen = strlen(orig) + 1; pastorigend = orig + newlen; for (p = orig; (q = strstr(p, "{}")) != NULL; p = q + 2) { if (plen > 2 && newlen + plen - 2 < newlen) errx(2, "brace_subst overflow"); newlen += plen - 2; } if (newlen > len) { *store = reallocf(*store, newlen); if (*store == NULL) err(2, NULL); } dst = *store; for (p = orig; (q = strstr(p, "{}")) != NULL; p = q + 2) { memcpy(dst, p, q - p); dst += q - p; memcpy(dst, path, plen); dst += plen; } memcpy(dst, p, pastorigend - p); }
/* Decode a hexadecimal string, set *len to length, in[] to the bytes. This decodes liberally, in that hex digits can be adjacent, in which case two in a row writes a byte. Or they can delimited by any non-hex character, where the delimiters are ignored except when a single hex digit is followed by a delimiter in which case that single digit writes a byte. The returned data is allocated and must eventually be freed. NULL is returned if out of memory. If the length is not needed, then len can be NULL. */ static unsigned char *h2b(const char *hex, unsigned *len) { unsigned char *in; unsigned next, val; in = malloc((strlen(hex) + 1) >> 1); if (in == NULL) return NULL; next = 0; val = 1; do { if (*hex >= '0' && *hex <= '9') val = (val << 4) + *hex - '0'; else if (*hex >= 'A' && *hex <= 'F') val = (val << 4) + *hex - 'A' + 10; else if (*hex >= 'a' && *hex <= 'f') val = (val << 4) + *hex - 'a' + 10; else if (val != 1 && val < 32) /* one digit followed by delimiter */ val += 240; /* make it look like two digits */ if (val > 255) { /* have two digits */ in[next++] = val & 0xff; /* save the decoded byte */ val = 1; /* start over */ } } while (*hex++); /* go through the loop with the terminating null */ if (len != NULL) *len = next; in = reallocf(in, next); return in; }
static ssize_t read_file(int fd, void **rv) { uint8_t *retval; size_t len; off_t off; ssize_t red; len = 4096; off = 0; retval = malloc(len); do { red = read(fd, retval + off, len - off); off += red; if (red < (ssize_t)(len - off)) break; len *= 2; retval = reallocf(retval, len); if (retval == NULL) return -1; } while (1); *rv = retval; return off; }
static void _internal_remove_controlled_name(notify_state_t *ns, name_info_t *n) { uint32_t i, j; for (i = 0; i < ns->controlled_name_count; i++) { if (ns->controlled_name[i] == n) { for (j = i + 1; j < ns->controlled_name_count; j++) { ns->controlled_name[j-1] = ns->controlled_name[j]; } ns->controlled_name_count--; if (ns->controlled_name_count == 0) { free(ns->controlled_name); ns->controlled_name = NULL; } else { ns->controlled_name = (name_info_t **)reallocf(ns->controlled_name, ns->controlled_name_count * sizeof(name_info_t *)); } return; } } }
int nzb_fetch_add_server(nzb_fetch *fetcher, char *address, int port, char *username, char *password, int threads, int ssl, int priority) { server_t *new_server, *server; int required_queues; int current_queues; if (priority < 0) { fprintf(stderr, "Error: priority should be > 0\n"); return -1; } #if !HAVE_LIBSSL if (ssl > 0) { fprintf(stderr, "Error: SSL Support is not compiled in\n"); return -1; } #endif new_server = server_create(address, port, username, password, threads, ssl, priority); // Insert into server linked list server = fetcher->servers; if(server == NULL) fetcher->servers = new_server; else { // Append server at the end while(server->next != NULL) server = server->next; server->next = new_server; new_server->prev = server; } required_queues = server_calc_priorities(fetcher); if (required_queues > 1) { current_queues = sizeof(fetcher->priority_queues) / sizeof(queue_list_t *); if (required_queues > current_queues) { fetcher->priority_queues = reallocf(fetcher->priority_queues, sizeof(queue_list_t *) * required_queues); fetcher->priority_queues[new_server->priority] = queue_list_create(); fetcher->priority_queues[new_server->priority]->id = strdup("priority queue"); } } return 0; }
/** internal, re-allocating snprintf helper */ void CGPathWriter_snprintf(CGPathWriter_t *const t, char *fmt, ...) { assert(t != NULL); for(;; ) { if( t->buffer == NULL ) return; assert(t->used >= 0); assert(t->allocated >= t->used); const size_t max = t->allocated - t->used; // http://stackoverflow.com/questions/498705/create-a-my-printf-that-sends-data-to-both-a-sprintf-and-the-normal-printf va_list ap; va_start(ap, fmt); const int len = vsnprintf(t->buffer + t->used, max, fmt, ap); va_end(ap); assert(len >= 0); if( len >= max ) { assert(t->increment >= 0); const size_t inc = MAX(len - max + 1, t->increment); assert(t->allocated < INT_MAX - inc); // fprintf(stderr, "CGPathToCString::re-allocating %zu\n", inc); t->buffer = reallocf(t->buffer, t->allocated += inc); assert(t->allocated > t->used); t->buffer[t->used] = '\0'; assert(t->buffer[t->used] == '\0'); } else { t->used += len; return; } } }
static int raosnprintf(char **buf, size_t *size, ssize_t *offset, char *fmt, ...) { va_list ap; int ret; do { if (*offset < *size) { va_start(ap, fmt); ret = vsnprintf(*buf + *offset, *size - *offset, fmt, ap); va_end(ap); if (ret < (*size - *offset)) { *offset += ret; return ret; } } *buf = reallocf(*buf, (*size *= 2)); } while (*buf); //warn("reallocf failure"); return 0; }
int aeron_reallocf(void **ptr, size_t size) { #if defined(__linux__) || defined(AERON_COMPILER_MSVC) /* mimic reallocf */ if ((*ptr = realloc(*ptr, size)) == NULL) { if (0 == size) { *ptr = NULL; } else { free(*ptr); errno = ENOMEM; return -1; } } #else if ((*ptr = reallocf(*ptr, size)) == NULL) { errno = ENOMEM; return -1; } #endif return 0; }
static FTSENT * fts_sort(FTS *sp, FTSENT *head, size_t nitems) { FTSENT **ap, *p; /* * Construct an array of pointers to the structures and call qsort(3). * Reassemble the array in the order returned by qsort. If unable to * sort for memory reasons, return the directory entries in their * current order. Allocate enough space for the current needs plus * 40 so don't realloc one entry at a time. */ if (nitems > sp->fts_nitems) { sp->fts_nitems = nitems + 40; if ((sp->fts_array = reallocf(sp->fts_array, sp->fts_nitems * sizeof(FTSENT *))) == NULL) { sp->fts_nitems = 0; return (head); } } for (ap = sp->fts_array, p = head; p; p = p->fts_link) *ap++ = p; if (ISSET(FTS_COMPAR_B)) qsort_r(sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar_b, fts_compar_b); else qsort(sp->fts_array, nitems, sizeof(FTSENT *), fts_compar); for (head = *(ap = sp->fts_array); --nitems; ++ap) ap[0]->fts_link = ap[1]; ap[0]->fts_link = NULL; return (head); }
u_char * __collate_substitute(const u_char *s) { int dest_len, len, nlen; int delta = strlen(s); u_char *dest_str = NULL; if (s == NULL || *s == '\0') return (__collate_strdup("")); delta += delta / 8; dest_str = malloc(dest_len = delta); if (dest_str == NULL) __collate_err(EX_OSERR, __func__); len = 0; while (*s) { nlen = len + strlen(__collate_substitute_table[*s]); if (dest_len <= nlen) { dest_str = reallocf(dest_str, dest_len = nlen + delta); if (dest_str == NULL) __collate_err(EX_OSERR, __func__); } strcpy(dest_str + len, __collate_substitute_table[*s++]); len = nlen; } return (dest_str); }
/* * Allow essentially unlimited paths; find, rm, ls should all work on any tree. * Most systems will allow creation of paths much longer than MAXPATHLEN, even * though the kernel won't resolve them. Add the size (not just what's needed) * plus 256 bytes so don't realloc the path 2 bytes at a time. */ static int fts_palloc(FTS *sp, size_t more) { sp->fts_pathlen += more + 256; sp->fts_path = reallocf(sp->fts_path, sp->fts_pathlen); return (sp->fts_path == NULL); }
void reallocfRadar6337483_4() { char *buf = malloc(100); char *buf2 = (char*)reallocf(buf, 0x1000000); if (!buf2) { return; // no warning - reallocf frees even on failure } else { free(buf2); } }
static void add_arg(struct captured_main_args *args, char *arg) { args->argc++; args->argv = reallocf(args->argv, (args->argc + 1) * sizeof(char *)); if (args->argv == NULL) err(1, "Out of memory building argument list"); args->argv[args->argc] = arg; }
void * _kvm_realloc(kvm_t *kd, void *p, size_t n) { void *np; np = reallocf(p, n); if (np == NULL) _kvm_err(kd, kd->program, "out of memory"); return (np); }
/* * Expands allocated memory to fit an additional TTY. * Two arrays are kept with matching indexes, one for ucom and one * for our private data. */ static int uhso_alloc_tty(struct uhso_softc *sc) { sc->sc_ttys++; sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO); if (sc->sc_tty == NULL) return (-1); sc->sc_ucom = reallocf(sc->sc_ucom, sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO); if (sc->sc_ucom == NULL) return (-1); sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc; UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1); return (sc->sc_ttys - 1); }
void reallocfRadar6337483_3() { char * buf = malloc(100); char * tmp; tmp = (char*)reallocf(buf, 0x1000000); if (!tmp) { free(buf); // expected-warning {{Attempt to free released memory}} return; } buf = tmp; free(buf); }
/* A realloc that checks errors */ void * safe_realloc(void *orig, size_t size) { void *ptr; if (size <= 0) return orig; ptr = reallocf(orig, size); if (!ptr) return orig; return ptr; }
/* * sl_add(): Add an item to the string list */ int sl_add(StringList *sl, char *name) { if (sl->sl_cur == sl->sl_max - 1) { sl->sl_max += _SL_CHUNKSIZE; sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); if (sl->sl_str == NULL) return (-1); } sl->sl_str[sl->sl_cur++] = name; return (0); }
static int add_host(int pref, const char *host, int port, struct mx_hostentry **he, size_t *ps) { struct addrinfo hints, *res, *res0 = NULL; char servname[10]; struct mx_hostentry *p; size_t onhosts; const int count_inc = 10; int err; onhosts = *ps; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; snprintf(servname, sizeof(servname), "%d", port); err = getaddrinfo(host, servname, &hints, &res0); if (err) return (-1); for (res = res0; res != NULL; res = res->ai_next) { if (*ps + 1 >= roundup(*ps, count_inc)) { size_t newsz = roundup(*ps + 2, count_inc); *he = reallocf(*he, newsz * sizeof(**he)); if (*he == NULL) goto out; } p = &(*he)[*ps]; strlcpy(p->host, host, sizeof(p->host)); p->pref = pref; p->ai = *res; p->ai.ai_addr = NULL; bcopy(res->ai_addr, &p->sa, p->ai.ai_addrlen); getnameinfo((struct sockaddr *)&p->sa, p->ai.ai_addrlen, p->addr, sizeof(p->addr), NULL, 0, NI_NUMERICHOST); (*ps)++; } freeaddrinfo(res0); return (*ps - onhosts); out: if (res0 != NULL) freeaddrinfo(res0); return (-1); }
int ifconfig_get_description(ifconfig_handle_t *h, const char *name, char **description) { struct ifreq ifr; char *descr; size_t descrlen; descr = NULL; descrlen = 64; memset(&ifr, 0, sizeof(ifr)); (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); for (;;) { if ((descr = reallocf(descr, descrlen)) == NULL) { h->error.errtype = OTHER; h->error.errcode = ENOMEM; return (-1); } ifr.ifr_buffer.buffer = descr; ifr.ifr_buffer.length = descrlen; if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFDESCR, &ifr) != 0) { free(descr); return (-1); } if (ifr.ifr_buffer.buffer == descr) { if (strlen(descr) > 0) { *description = strdup(descr); free(descr); if (description == NULL) { h->error.errtype = OTHER; h->error.errcode = ENOMEM; return (-1); } return (0); } } else if (ifr.ifr_buffer.length > descrlen) { descrlen = ifr.ifr_buffer.length; continue; } break; } free(descr); h->error.errtype = OTHER; h->error.errcode = 0; return (-1); }
void append_item(os_pointer item, gc_list* list) { assert(item); if(list->highest_entry == list->max_entry_count) { list->max_entry_count *= 2; list->list_entries = (os_pointer*)reallocf(list->list_entries, sizeof(os_pointer)*list->max_entry_count); } list->list_entries[list->highest_entry] = item; ++list->highest_entry; ++list->entry_count; }
Pkg *pkg_db_get_installed_matched(PkgDb db, const char *pattern){ Pkg *pkgs; struct dirent *dirn; size_t size; int count = 0; if(db == NULL || db->db_d == NULL || db->d_h == NULL) RETURN_P_ERR(P_ERR_INVALID_DESCRIPTOR, NULL); size = sizeof(Pkg); pkgs = malloc(size); assert(pkgs != NULL); pkgs[0] = NULL; while((dirn = readdir(db->d_h)) != NULL){ if(pattern != NULL){ if(!strncmp(pattern, dirn->d_name, strlen(pattern)) && dirn->d_type == DT_DIR && dirn->d_name[0] != '.'){ size += sizeof(Pkg); pkgs = reallocf(pkgs, size); assert(pkgs != NULL); pkgs[count++] = pkg_new(db->db_d, dirn->d_name); pkgs[count] = NULL; } } else if(dirn->d_type == DT_DIR && dirn->d_name[0] != '.'){ size += sizeof(Pkg); pkgs = reallocf(pkgs, size); assert(pkgs != NULL); pkgs[count++] = pkg_new(db->db_d, dirn->d_name); pkgs[count] = NULL; } } return pkgs; }
void asl_msg_list_insert(asl_msg_list_t *list, uint32_t x, void *obj) { uint32_t i, j; asl_object_private_t *oo = (asl_object_private_t *)obj; if (list == NULL) return; if (obj == NULL) return; if (list->count == UINT32_MAX) return; if (x >= list->count) x = list->count; uint32_t type = asl_get_type((asl_object_t)oo); uint32_t count = 0; if ((type == ASL_TYPE_MSG) || (type == ASL_TYPE_QUERY)) count = 1; else count = asl_object_count(oo); if (count == 0) return; uint64_t check = list->count; check += count; if (check > UINT32_MAX) return; list->msg = (asl_msg_t **)reallocf(list->msg, (list->count + count) * sizeof(asl_msg_t *)); if (list->msg == NULL) { list->count = 0; list->curr = 0; return; } for (i = list->count, j = i - 1; i > x; i--, j--) list->msg[i] = list->msg[j]; asl_object_set_iteration_index(oo, 0); if ((type == ASL_TYPE_MSG) || (type == ASL_TYPE_QUERY)) { list->msg[x] = (asl_msg_t *)asl_retain((asl_object_t)oo); } else { for (i = x, j = 0; j < count; i++, j++) list->msg[i] = (asl_msg_t *)asl_object_next(oo); } asl_object_set_iteration_index(oo, 0); list->count += count; }
static int map_iter(const rd_loadobj_t *lop, void *arg) { struct proc_handle *phdl = arg; if (phdl->nobjs >= phdl->rdobjsz) { phdl->rdobjsz *= 2; phdl->rdobjs = reallocf(phdl->rdobjs, sizeof(*phdl->rdobjs) * phdl->rdobjsz); if (phdl->rdobjs == NULL) return (-1); } memcpy(&phdl->rdobjs[phdl->nobjs++], lop, sizeof(*lop)); return (0); }