double * Pvalue_adjust_StepDown ( const double * pval, const int n, const int method){ int i; double * pval_adj,**work; assert(IsPvals(pval,n)); assert(method==BONFERRONI || method==SIDAK); pval_adj = calloc(n,sizeof(double)); OOM(pval_adj); work = calloc(n,sizeof(double *)); OOM(work); for ( i=0 ; i<n ; i++){ pval_adj[i] = pval[i]; work[i] = &pval_adj[i]; } qsort (work,n,sizeof(double *),CmpDoublePtr); *work[0] = CorrectPval(*work[0],n,method); for ( i=1 ; i<n ; i++){ *work[i] = CorrectPval(*work[i],n-i,method); *work[i] = (*work[i]>*work[i-1])?(*work[i]):(*work[i-1]); } free(work); assert(IsPvals(pval_adj,n)); return pval_adj; }
static void ExtendNode ( NODE * node){ NODE ** new_branch; double * new_blength; int bran; assert(NULL!=node); assert(node->maxbran>0); new_branch = calloc(2*node->maxbran,sizeof(NODE *)); OOM(new_branch); new_blength = calloc(2*node->maxbran,sizeof(double)); OOM(new_blength); assert(node->nbran>=0); assert(node->nbran<=node->maxbran); for ( bran=0 ; bran<node->nbran ; bran++){ new_branch[bran] = node->branch[bran]; new_blength[bran] = node->blength[bran]; } for ( ; bran<2*node->maxbran ; bran++){ new_branch[bran] = NULL; new_blength[bran] = -1.; } node->maxbran *= 2; free(node->branch); free(node->blength); node->branch = new_branch; node->blength = new_blength; }
Bool addToList(list_t *list, const char *newVal) { register int i; if ((!newVal) || (!newVal[0])) { list->num = 0; return True; } for (i = 0; i < list->num; i++) { if (streq(list->item[i], newVal)) return True; } if ((list->item == NULL) || (list->sz < 1)) { list->num = 0; list->sz = 4; list->item = (char **) calloc(list->sz, sizeof(char *)); OOM(list->item); } else if (list->num >= list->sz) { list->sz *= 2; list->item = (char **) realloc(list->item, (list->sz) * sizeof(char *)); OOM(list->item); } list->item[list->num] = strdup(newVal); OOM(list->item[list->num]); list->num += 1; return True; }
double * Pvalue_adjust_StepUp ( const double * pval, const int n, const int method){ int i; double * pval_adj,**work; assert(IsPvals(pval,n)); if(method==SIDAK){ fputs("Using Sidak adjustment with Hochberg\'s step-up proceedure.\nMay not be valid -- check theory!\n",stderr); } pval_adj = calloc(n,sizeof(double)); OOM(pval_adj); work = calloc(n,sizeof(double *)); OOM(work); for ( i=0 ; i<n ; i++){ pval_adj[i] = pval[i]; work[i] = &pval_adj[i]; } qsort (work,n,sizeof(double *),CmpDoublePtr); for ( i=(n-2) ; i>=0 ; i--){ *work[i] = CorrectPval( *work[i],n-i,method); *work[i] = (*work[i]<*work[i+1])?(*work[i]):(*work[i+1]); } free(work); assert(IsPvals(pval_adj,n)); return pval_adj; }
static int make_ipv4(char **argval) { struct in6_addr addr; struct in_addr src; char buf[INET_ADDRSTRLEN + 1], *str; memset(&addr, 0, sizeof(addr)); /* already verified */ inet_pton(AF_INET6, *argval, &addr); src.s_addr = htonl((addr.s6_addr[12] << 24) | (addr.s6_addr[13] << 16) | (addr.s6_addr[14] << 8) | (addr.s6_addr[15])); inet_ntop(AF_INET, &src, buf, sizeof(buf)); buf[sizeof(buf) - 1] = 0; str = strdup(buf); if (!str) return OOM(); free(*argval); *argval = str; return 0; }
NODE *CreateNode (void) { NODE *node; int i; node = malloc (sizeof (NODE)); OOM (node); node->plik = NULL; node->seq = NULL; node->name = NULL; node->mat = NULL; node->back = NULL; node->mid = NULL; node->bmat = NULL; node->dback = NULL; node->bnumber = -1; node->nbran = 0; node->maxbran = 3; node->blength = calloc(3,sizeof(double)); node->branch = calloc(3,sizeof(NODE *)); for ( i=0 ; i<node->maxbran ; i++){ node->blength[i] = -1.; node->branch[i] = NULL; } return node; }
TEST test_zset_oom() { INIT(); RL_CALL_VERBOSE(rl_zadd, RL_OK, db, key, keylen, 1.23, UNSIGN("a"), 1); RL_CALL_VERBOSE(rl_zadd, RL_OK, db, key, keylen, 4.5, UNSIGN("b"), 1); OOM(); }
TEST test_hash_oom() { INIT(); RL_CALL_VERBOSE(rl_hset, RL_OK, db, key, keylen, UNSIGN("field"), 5, UNSIGN("value"), 5, NULL, 0); RL_CALL_VERBOSE(rl_hset, RL_OK, db, key, keylen, UNSIGN("field2"), 6, UNSIGN("value2"), 6, NULL, 0); OOM(); }
TEST test_list_oom() { INIT(); unsigned char *values[2] = {UNSIGN("b"), UNSIGN("a")}; long valueslen[2] = {1, 1}; RL_CALL_VERBOSE(rl_push, RL_OK, db, key, keylen, 1, 0, 2, values, valueslen, NULL); OOM(); }
char * stringFromOptions(char *orig, list_t *newOpts) { int len, i, nOut; if (orig) len = strlen(orig) + 1; else len = 0; for (i = 0; i < newOpts->num; i++) { if (newOpts->item[i]) len += strlen(newOpts->item[i]) + 1; } if (len < 1) return NULL; if (orig) { orig = (char *) realloc(orig, len); OOM(orig); nOut = 1; } else { orig = (char *) calloc(len, 1); OOM(orig); nOut = 0; } for (i = 0; i < newOpts->num; i++) { if (!newOpts->item[i]) continue; if (nOut > 0) { strcat(orig, ","); strcat(orig, newOpts->item[i]); } else strcpy(orig, newOpts->item[i]); nOut++; } return orig; }
JNIEXPORT jlong JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_init( JNIEnv *jenv, jobject jobj, jstring juseragent) { char *useragent; struct libctx *ctx = calloc(1, sizeof(*ctx)); if (!ctx) goto bad; ctx->jenv = jenv; ctx->jobj = (*jenv)->NewGlobalRef(jenv, jobj); if (!ctx->jobj) goto bad_free_ctx; ctx->async_lock = init_async_lock(ctx); if (!ctx->async_lock) goto bad_delete_obj_ref; useragent = (char *)(*jenv)->GetStringUTFChars(jenv, juseragent, NULL); if (!useragent) goto bad_delete_ref; ctx->vpninfo = openconnect_vpninfo_new(useragent, validate_peer_cert_cb, write_new_config_cb, process_auth_form_cb, progress_cb, ctx); (*jenv)->ReleaseStringUTFChars(jenv, juseragent, useragent); if (!ctx->vpninfo) goto bad_delete_ref; openconnect_set_token_callbacks(ctx->vpninfo, ctx, lock_token_cb, unlock_token_cb); openconnect_set_protect_socket_handler(ctx->vpninfo, protect_socket_cb); openconnect_set_stats_handler(ctx->vpninfo, stats_cb); openconnect_set_setup_tun_handler(ctx->vpninfo, setup_tun_cb); openconnect_set_reconnected_handler(ctx->vpninfo, reconnected_cb); ctx->cmd_fd = openconnect_setup_cmd_pipe(ctx->vpninfo); if (ctx->cmd_fd < 0) goto bad_free_vpninfo; ctx->loglevel = PRG_DEBUG; return (jlong)(unsigned long)ctx; bad_free_vpninfo: openconnect_vpninfo_free(ctx->vpninfo); bad_delete_ref: (*jenv)->DeleteGlobalRef(jenv, ctx->async_lock); bad_delete_obj_ref: (*jenv)->DeleteGlobalRef(jenv, ctx->jobj); bad_free_ctx: free(ctx); bad: OOM(jenv); return 0; }
void SetDefaultOptions (void) { int i; assert (NULL!=optiondefault); if (opt == NULL) { opt = malloc (sizeof (OPTIONS)); OOM(opt); opt->val = malloc (n_options * sizeof (void *)); OOM(opt->val); for (i = 0; i < n_options; i++) opt->val[i] = NULL; } for (i = 0; i < n_options; i++) AddOption (i, optiondefault[i]); return; }
static void double_length_of_mystring (Mystring string){ char * new_mem; check_is_mystring(string); new_mem = malloc(string->maxlen * 2 * sizeof(char)); OOM(new_mem); memcpy (new_mem,string->string,string->len * sizeof(char)); free(string->string); string->string = new_mem; string->maxlen *= 2; check_is_mystring (string); }
struct cpu_bitmask * cpu_bitmask_new(struct cpuset *set) { struct cpu_bitmask *bmask; BUG_ON(!num_cpus); BUG_ON(!set); bmask = g_malloc0(sizeof(struct cpu_bitmask) + CPUSET_SIZE(num_cpus) / 8); if (bmask) { bmask->cpuset = set; bmask->len = CPUSET_SIZE(num_cpus) / 8; } else OOM(); return bmask; }
double * Pvalue_adjust_SingleStep ( const double * pval, const int n, const int method){ int i; double * pval_adj; assert(IsPvals(pval,n)); assert(method==BONFERRONI || method==SIDAK); pval_adj = calloc(n,sizeof(double)); OOM(pval_adj); for ( i=0 ; i<n ; i++){ pval_adj[i] = CorrectPval(pval[i],n,method); } assert( IsPvals(pval_adj,n)); return pval_adj; }
/* special handling: caller-allocated buffer */ JNIEXPORT jstring JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPeerCertHash( JNIEnv *jenv, jobject jobj) { struct libctx *ctx = getctx(jenv, jobj); const char *hash; jstring jresult = NULL; if (!ctx) return NULL; hash = openconnect_get_peer_cert_hash(ctx->vpninfo); if (!hash) return NULL; jresult = dup_to_jstring(ctx->jenv, hash); if (!jresult) OOM(ctx->jenv); return jresult; }
static int get_cstring(JNIEnv *jenv, jstring in, const char **out) { const char *tmp; if (in == NULL) { *out = NULL; return 0; } tmp = (*jenv)->GetStringUTFChars(jenv, in, NULL); if (!tmp) { OOM(jenv); return -1; } *out = tmp; return 0; }
/* special handling: callee-allocated, caller-freed string */ JNIEXPORT jstring JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPeerCertDetails( JNIEnv *jenv, jobject jobj) { struct libctx *ctx = getctx(jenv, jobj); char *buf = NULL; jstring jresult = NULL; if (!ctx) return NULL; buf = openconnect_get_peer_cert_details(ctx->vpninfo); if (!buf) return NULL; jresult = dup_to_jstring(ctx->jenv, buf); if (!jresult) OOM(ctx->jenv); openconnect_free_cert_info(ctx->vpninfo, buf); return jresult; }
static NODE *CloneTree_sub (const NODE * node, const NODE * parent, const TREE * tree, TREE * tree_new) { NODE *node_new; int ln, n; int bufflen; node_new = CreateNode (); OOM (node_new); node_new->bnumber = node->bnumber; n = 0; while (node->branch[n] != NULL) { if (node->branch[n] != parent) { node_new->branch[n] = CloneTree_sub (node->branch[n], node, tree, tree_new); // Connect all children to parent (node_new->branch[n])->branch[0] = node_new; } node_new->blength[n] = node->blength[n]; n++; } node_new->branch[n] = NULL; // If on a leaf, then update leaves index if (ISLEAF (node)) { bufflen = 1 + strlen(node->name); node_new->name = malloc(bufflen*sizeof(char)); strncpy(node_new->name,node->name,bufflen); insertelt_rbtree(tree_new->leaves,node_new->name,node_new); } // If not at root node, then in branch index if (NULL != parent) { ln = find_branch_number (node, tree); assert (node_new->bnumber == ln); tree_new->branches[ln] = node_new; } return node_new; }
void UDPCallbacks::on_recv(ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags) { if (nread == 0) return; jobject buffer_arg = NULL; if (nread > 0) { jbyte* data = new jbyte[nread]; memcpy(data, buf.base, nread); buffer_arg = _env->NewDirectByteBuffer(data, nread); OOM(_env, buffer_arg); } jobject rinfo_arg = addr ? StreamCallbacks::_address_to_js(_env, addr) : NULL; _env->CallVoidMethod( _instance, _recv_callback_mid, nread, buffer_arg, rinfo_arg); if (buffer_arg) { _env->DeleteLocalRef(buffer_arg); } delete[] buf.base; }
Bool addStringToOptions(char *opt_str, list_t *opts) { char *tmp, *str, *next; Bool ok = True; str = strdup(opt_str); OOM(str); for (tmp = str; (tmp && *tmp != '\0') && ok; tmp = next) { next = strchr(str, ','); if (next) { *next = '\0'; next++; } ok = addToList(opts, tmp) && ok; } free(str); return ok; }
progress_cb(void *privdata, int level, const char *fmt, ...) { struct libctx *ctx = privdata; va_list ap; char *msg; jstring jmsg; int ret, loglevel; jmethodID mid; (*ctx->jenv)->MonitorEnter(ctx->jenv, ctx->async_lock); loglevel = ctx->loglevel; (*ctx->jenv)->MonitorExit(ctx->jenv, ctx->async_lock); if (level > loglevel) return; va_start(ap, fmt); ret = vasprintf(&msg, fmt, ap); va_end(ap); if (ret < 0) { OOM(ctx->jenv); return; } if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0) return; jmsg = dup_to_jstring(ctx->jenv, msg); free(msg); if (!jmsg) goto out; mid = get_obj_mid(ctx, ctx->jobj, "onProgress", "(ILjava/lang/String;)V"); if (mid) (*ctx->jenv)->CallVoidMethod(ctx->jenv, ctx->jobj, mid, level, jmsg); out: (*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL); }
void init_buffer(buffer *b, int l) { b->data = malloc(l); if (!b->data) OOM(); b->size = 0; b->maxsize = l; }
int owfd_dhcp_parse_argv(struct owfd_dhcp_config *conf, int argc, char **argv) { int c; bool help = false; char *t; int r; opterr = 0; while (1) { c = getopt_long(argc, argv, short_options, long_options, NULL); if (c <= 0) { break; } else if (c == ':') { fprintf(stderr, "missing argument for: %s\n", argv[optind - 1]); return -EINVAL; } else if (c == '?') { if (optopt && optopt < LONG_OPT_OFFSET) fprintf(stderr, "unknown argument: -%c\n", optopt); else if (!optopt) fprintf(stderr, "unknown argument: %s\n", argv[optind - 1]); else fprintf(stderr, "option takes no arg: %s\n", argv[optind - 1]); return -EINVAL; } #define OPT(_num) LONG_OPT_OFFSET + _num switch (c) { case 'h': case OPT(OPT_HELP): help = true; break; case 'v': case OPT(OPT_VERBOSE): conf->verbose = 1; break; case OPT(OPT_SILENT): conf->silent = 1; break; case OPT(OPT_DEBUG): conf->debug = 1; break; case 'c': case OPT(OPT_CLIENT): conf->server = 0; conf->client = 1; break; case 's': case OPT(OPT_SERVER): conf->client = 0; conf->server = 1; break; case '4': case OPT(OPT_IPV4): conf->ipv6 = 0; conf->ipv4 = 1; break; case '6': case OPT(OPT_IPV6): conf->ipv4 = 0; conf->ipv6 = 1; break; case 'i': case OPT(OPT_INTERFACE): t = strdup(optarg); if (!t) return OOM(); free(conf->interface); conf->interface = t; break; case OPT(OPT_IP_BINARY): t = strdup(optarg); if (!t) return OOM(); free(conf->ip_binary); conf->ip_binary = t; break; case OPT(OPT_LOCAL): t = strdup(optarg); if (!t) return OOM(); free(conf->local); conf->local = t; break; case OPT(OPT_GATEWAY): t = strdup(optarg); if (!t) return OOM(); free(conf->gateway); conf->gateway = t; break; case OPT(OPT_DNS): t = strdup(optarg); if (!t) return OOM(); free(conf->dns); conf->dns = t; break; case OPT(OPT_SUBNET): t = strdup(optarg); if (!t) return OOM(); free(conf->subnet); conf->subnet = t; break; case OPT(OPT_IP_FROM): t = strdup(optarg); if (!t) return OOM(); free(conf->ip_from); conf->ip_from = t; break; case OPT(OPT_IP_TO): t = strdup(optarg); if (!t) return OOM(); free(conf->ip_to); conf->ip_to = t; break; } #undef OPT } if (help) { show_help(); return -EAGAIN; } if (optind < argc) { fprintf(stderr, "unparsed remaining arguments starting with: %s\n", argv[optind]); return -EINVAL; } if (!conf->client && !conf->server) { fprintf(stderr, "no --client or --server given\n"); return -EINVAL; } if (!conf->ipv4 && !conf->ipv6) { fprintf(stderr, "no --ipv4 or --ipv6 given\n"); return -EINVAL; } if (conf->ipv6) { fprintf(stderr, "--ipv6 not implemented by gdhcp, yet\n"); return -EINVAL; } if (!conf->interface) { fprintf(stderr, "no interface given, use: -i <iface>\n"); return -EINVAL; } if (!conf->ip_binary) { conf->ip_binary = strdup(BUILD_BINDIR_IP "/ip"); if (!conf->ip_binary) return OOM(); } if (conf->server) { r = verify_address("--local", conf->local, conf->ipv4); if (r >= 0 && conf->ipv4) r = make_ipv4(&conf->local); if (r < 0) return r; r = verify_address("--gateway", conf->gateway, conf->ipv4); if (r >= 0 && conf->ipv4) r = make_ipv4(&conf->gateway); if (r < 0) return r; r = verify_address("--dns", conf->dns, conf->ipv4); if (r >= 0 && conf->ipv4) r = make_ipv4(&conf->dns); if (r < 0) return r; r = verify_address("--subnet", conf->subnet, conf->ipv4); if (r >= 0 && conf->ipv4) r = make_ipv4(&conf->subnet); if (r < 0) return r; r = verify_address("--ip-from", conf->ip_from, conf->ipv4); if (r >= 0 && conf->ipv4) r = make_ipv4(&conf->ip_from); if (r < 0) return r; r = verify_address("--ip-to", conf->ip_to, conf->ipv4); if (r >= 0 && conf->ipv4) r = make_ipv4(&conf->ip_to); if (r < 0) return r; } return 0; }
int make_net_info(const char *disc_cmd, const char *info_path) { int rc = -1; char *stmp_path = NULL; mode_t stmp_mode = 0644; int stmp_fd = -1; FILE *stmp_file = NULL; FILE *disc_file = NULL; size_t stmp_path_size = strlen(info_path) + 10; stmp_path = malloc(stmp_path_size); if (stmp_path == NULL) OOM(); snprintf(stmp_path, stmp_path_size, "%s.XXXXXXXX", info_path); stmp_fd = mkstemp(stmp_path); if (stmp_fd < 0) { ERROR("cannot open temporary file `%s': %m\n", stmp_path); goto out; } if (fchmod(stmp_fd, stmp_mode) < 0) { ERROR("cannot chmod `%s': %m\n", stmp_path); goto out; } stmp_file = fdopen(stmp_fd, "w"); if (stmp_file == NULL) { ERROR("cannot open `%s': %m\n", stmp_path); goto out; } stmp_fd = -1; disc_file = popen(disc_cmd, "r"); if (disc_file == NULL) { ERROR("cannot execute `%s': %m\n", disc_cmd); goto out; } if (net_disc_to_info(disc_file, stmp_file) < 0) goto out; rc = 0; out: if (disc_file != NULL) { int st = pclose(disc_file); if (st != 0) { ERROR("command `%s' exited with status %d\n", disc_cmd, st); rc = -1; } } if (stmp_file != NULL) { if (fclose(stmp_file) != 0) { ERROR("error closing `%s': %m\n", stmp_path); rc = -1; } } if (rc == 0 && rename(stmp_path, info_path) < 0) { ERROR("cannot rename `%s' to `%s': %m\n", stmp_path, info_path); rc = -1; } if (stmp_fd >= 0) close(stmp_fd); free(stmp_path); return rc; }
static int process_auth_form_cb(void *privdata, struct oc_auth_form *form) { struct libctx *ctx = privdata; jobject jform; jmethodID callback, getOptValue; struct oc_form_opt *opt; jint ret; if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0) return -1; /* create and populate new AuthForm object and option/choice lists */ jform = new_auth_form(ctx, form); if (!jform) goto err; getOptValue = get_obj_mid(ctx, jform, "getOptValue", "(Ljava/lang/String;)Ljava/lang/String;"); if (!getOptValue) goto err; for (opt = form->opts; opt; opt = opt->next) { int is_authgroup = opt == (void *)form->authgroup_opt; if (add_form_option(ctx, jform, opt, is_authgroup) < 0) goto err; } /* invoke onProcessAuthForm callback */ callback = get_obj_mid(ctx, ctx->jobj, "onProcessAuthForm", "(Lorg/infradead/libopenconnect/LibOpenConnect$AuthForm;)I"); if (!callback) goto err; ret = (*ctx->jenv)->CallIntMethod(ctx->jenv, ctx->jobj, callback, jform); /* copy any populated form fields back into the C structs */ for (opt = form->opts; opt; opt = opt->next) { jstring jname, jvalue; jname = dup_to_jstring(ctx->jenv, opt->name); if (!jname) goto err; jvalue = (*ctx->jenv)->CallObjectMethod(ctx->jenv, jform, getOptValue, jname); if (jvalue) { const char *tmp = (*ctx->jenv)->GetStringUTFChars(ctx->jenv, jvalue, NULL); if (!tmp) goto err; if (opt->type == OC_FORM_OPT_SELECT) opt->_value = lookup_choice_name((void *)opt, tmp); else { free(opt->_value); opt->_value = strdup(tmp); if (!opt->_value) OOM(ctx->jenv); } (*ctx->jenv)->ReleaseStringUTFChars(ctx->jenv, jvalue, tmp); } } (*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL); return ret; err: (*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL); return -1; }
TEST test_string_oom() { INIT(); RL_CALL_VERBOSE(rl_set, RL_OK, db, key, keylen, UNSIGN("asd"), 3, 0, 0); OOM(); }