void prepare_config(struct test_config *c, char *cpath, char *fpath)
{
- int tmp;
int count = 0;
struct test_function *f;
struct test_binding *b;
int i;
- tmp = asprintf(&c->name, "%s.%d",
- c->label, c->id);
- if (tmp < 0)
- fail();
- free_later(c->name);
+ safe_asprintf(&c->name, "%s.%d", c->label, c->id);
c->path = cpath;
for (f = c->bound_funcs; f->instance; f++)
count++;
- c->bindings = calloc(count + 1, sizeof(*c->bindings));
- if (c->bindings == NULL)
- fail();
- free_later(c->bindings);
+ c->bindings = safe_calloc(count + 1, sizeof(*c->bindings));
} else {
for (b = c->bindings; b->name; b++)
count++;
void prepare_function(struct test_function *f, char *path)
{
const char *func_type;
- int tmp;
func_type = usbg_get_function_type_str(f->type);
if (func_type == NULL)
fail();
- tmp = asprintf(&f->name, "%s.%s",
- func_type, f->instance);
- if (tmp < 0)
- fail();
- free_later(f->name);
+ safe_asprintf(&f->name, "%s.%s", func_type, f->instance);
f->path = path;
}
struct test_function *f;
char *fpath;
char *cpath;
- int tmp;
int count;
g->path = strdup(state->path);
free_later(g->path);
- tmp = asprintf(&fpath, "%s/%s/functions",
- g->path, g->name);
- if (tmp < 0)
- fail();
- free_later(fpath);
+ safe_asprintf(&fpath, "%s/%s/functions", g->path, g->name);
count = 0;
for (f = g->functions; f->instance; f++) {
qsort(g->functions, count, sizeof(*g->functions),
(int (*)(const void *, const void *))test_function_cmp);
- tmp = asprintf(&cpath, "%s/%s/configs",
- g->path, g->name);
- if (tmp < 0)
- fail();
- free_later(cpath);
+ safe_asprintf(&cpath, "%s/%s/configs", g->path, g->name);
count = 0;
for (c = g->configs; c->label; c++) {
for (f = functions; f->instance; ++f)
++count;
- new_functions = calloc(count + 1, sizeof(*f));
- if (!new_functions)
- fail();
- free_later(new_functions);
+ new_functions = safe_calloc(count + 1, sizeof(*f));
for (f = functions, nf = new_functions; f->instance; ++f, ++nf)
cpy_test_function(nf, f);
if (which < old || ((which - old) > count)) {
/* We may need to do a deep copy */
if (!which->writable) {
- ret = calloc(1, sizeof(*ret));
- if (!ret)
- fail();
- free_later(ret);
-
+ ret = safe_calloc(1, sizeof(*ret));
cpy_test_function(ret, which);
} else {
ret = which;
for (b = bindings; b->name; ++b)
++count;
- new_bindings = calloc(count + 1, sizeof(*b));
- if (!new_bindings)
- fail();
- free_later(new_bindings);
+ new_bindings = safe_calloc(count + 1, sizeof(*b));
for (b = bindings, nb = new_bindings; b->name; ++b, ++nb)
cpy_test_binding(nb, b, old, func_count, new);
for (c = configs; c->label; ++c)
++count;
- new_configs = calloc(count + 1, sizeof(*c));
- if (!new_configs)
- fail();
- free_later(new_configs);
+ new_configs = safe_calloc(count + 1, sizeof(*c));
for (c = configs, nc = new_configs; c->label; ++c, ++nc)
cpy_test_config(nc, c);
for (g = gadgets; g->name; ++g)
++count;
- new_gadgets = calloc(count + 1, sizeof(*g));
- if (!new_gadgets)
- fail();
- free_later(new_gadgets);
+ new_gadgets = safe_calloc(count + 1, sizeof(*g));
for (g = gadgets, ng = new_gadgets; g->name; ++g, ++ng)
cpy_test_gadget(ng, g);
struct test_state *new_state;
struct test_gadget *g;
- new_state = calloc(1, sizeof(*new_state));
- if (!new_state)
- fail();
- free_later(new_state);
+ new_state = safe_calloc(1, sizeof(*new_state));
/* We don't copy configfs path because it is never changed
if you would like to free it before test end replace
struct test_gadget *g;
struct test_state *new_state;
int count = 0;
- int tmp;
if (!state->writable)
new_state = dup_test_state(state);
else
new_state = state;
- tmp = asprintf(&(new_state->path), "%s/usb_gadget",
- new_state->configfs_path);
- if (tmp < 0)
- fail();
- free_later(new_state->path);
-
+ safe_asprintf(&(new_state->path), "%s/usb_gadget",
+ new_state->configfs_path);
for (g = new_state->gadgets; g->name; g++) {
prepare_gadget(new_state, g);
static void push_binding(struct test_config *conf, struct test_binding *binding)
{
- int tmp;
char *s_path;
char *d_path;
- tmp = asprintf(&s_path, "%s/%s/%s", conf->path, conf->name, binding->name);
- if (tmp < 0)
- fail();
- free_later(s_path);
-
- tmp = asprintf(&d_path, "%s/%s", binding->target->path, binding->target->name);
- if (tmp < 0)
- fail();
- free_later(d_path);
+ safe_asprintf(&s_path, "%s/%s/%s", conf->path, conf->name, binding->name);
+ safe_asprintf(&d_path, "%s/%s", binding->target->path, binding->target->name);
PUSH_LINK(s_path, d_path, USBG_MAX_PATH_LENGTH - 1);
}
{
struct test_binding *b;
int count = 0;
- int tmp;
char *path;
- tmp = asprintf(&path, "%s/%s", c->path, c->name);
- if (tmp < 0)
- fail();
- free_later(path);
+ safe_asprintf(&path, "%s/%s", c->path, c->name);
for (b = c->bindings; b->name; b++)
count++;
int count;
struct test_config *c;
struct test_function *f;
- int tmp;
char *path;
- tmp = asprintf(&path, "%s/%s/UDC", g->path, g->name);
- if (tmp < 0)
- fail();
- free_later(path);
+ safe_asprintf(&path, "%s/%s/UDC", g->path, g->name);
PUSH_FILE(path, g->udc);
count = 0;
{
char *path;
char *content;
- int tmp;
- tmp = asprintf(&path, "%s/%s/%s",
+ safe_asprintf(&path, "%s/%s/%s",
gadget->path, gadget->name, usbg_get_gadget_attr_str(attr));
- if (tmp >= USBG_MAX_PATH_LENGTH)
- fail();
- free_later(path);
- tmp = asprintf(&content, "0x%x\n", value);
- if (tmp < 0)
- fail();
- free_later(content);
+ safe_asprintf(&content, "0x%x\n", value);
EXPECT_HEX_WRITE(path, content);
}
{
char *path;
char *content;
- int tmp;
- tmp = asprintf(&path, "%s/%s/%s",
+ safe_asprintf(&path, "%s/%s/%s",
gadget->path, gadget->name, usbg_get_gadget_attr_str(attr));
- if (tmp < 0)
- fail();
- free_later(path);
-
- tmp = asprintf(&content, "0x%x\n", value);
- if (tmp < 0)
- fail();
- free_later(content);
+ safe_asprintf(&content, "0x%x\n", value);
PUSH_FILE(path, content);
}
{
char *path;
char *content;
- int tmp;
- tmp = asprintf(&path, "%s/%s/%s", config->path, config->name, config_attr_names[attr]);
- if (tmp < 0)
- fail();
- free_later(path);
+ safe_asprintf(&path, "%s/%s/%s", config->path, config->name, config_attr_names[attr]);
switch (config_attr_format[attr]) {
case FORMAT_HEX:
- tmp = asprintf(&content, "0x%x\n", value);
+ safe_asprintf(&content, "0x%x\n", value);
break;
case FORMAT_DEC:
- tmp = asprintf(&content, "%d\n", value);
+ safe_asprintf(&content, "%d\n", value);
break;
}
- if (tmp < 0)
- fail();
- free_later(content);
-
PUSH_FILE(path, content);
}
{
char *path;
char *content;
- int tmp;
- tmp = asprintf(&path, "%s/%s/%s", config->path, config->name, config_attr_names[attr]);
- if (tmp < 0)
- fail();
- free_later(path);
+ safe_asprintf(&path, "%s/%s/%s", config->path, config->name, config_attr_names[attr]);
switch (config_attr_format[attr]) {
case FORMAT_HEX:
- tmp = asprintf(&content, "0x%x\n", value);
+ safe_asprintf(&content, "0x%x\n", value);
break;
case FORMAT_DEC:
- tmp = asprintf(&content, "%d\n", value);
+ safe_asprintf(&content, "%d\n", value);
break;
}
- if (tmp < 0)
- fail();
- free_later(content);
-
switch (config_attr_format[attr]) {
case FORMAT_HEX:
EXPECT_HEX_WRITE(path, content);
{
char *dir;
int tmp;
- tmp = asprintf(&dir, "%s/%s/strings/0x%x",
+
+ safe_asprintf(&dir, "%s/%s/strings/0x%x",
gadget->path, gadget->name, lang);
- if (tmp < 0)
- fail();
- free_later(dir);
srand(time(NULL));
tmp = rand() % 2;
int lang, const char *content)
{
char *path;
- int tmp;
- tmp = asprintf(&path, "%s/%s/strings/0x%x/%s",
+ safe_asprintf(&path, "%s/%s/strings/0x%x/%s",
gadget->path, gadget->name, lang, attr_name);
- if (tmp < 0)
- fail();
- free_later(path);
EXPECT_WRITE(path, content);
}
static void push_gadget_str_dir(struct test_gadget *gadget, int lang)
{
char *dir;
- int tmp;
- tmp = asprintf(&dir, "%s/%s/strings/0x%x",
+
+ safe_asprintf(&dir, "%s/%s/strings/0x%x",
gadget->path, gadget->name, lang);
- if (tmp < 0)
- fail();
- free_later(dir);
EXPECT_OPENDIR(dir);
}
int lang, const char *content)
{
char *path;
- int tmp;
- tmp = asprintf(&path, "%s/%s/strings/0x%x/%s",
+ safe_asprintf(&path, "%s/%s/strings/0x%x/%s",
gadget->path, gadget->name, lang, attr_name);
- if (tmp < 0)
- fail();
- free_later(path);
PUSH_FILE(path, content);
}
int tmp;
- tmp = asprintf(&path, "%s/%s/strings/0x%x",
+ safe_asprintf(&path, "%s/%s/strings/0x%x",
config->path, config->name, lang);
- if (tmp < 0)
- fail();
- free_later(path);
srand(time(NULL));
tmp = rand() % 2;
EXPECT_MKDIR(path);
}
- tmp = asprintf(&path, "%s/configuration", path);
- if (tmp < 0)
- fail();
- free_later(path);
+ safe_asprintf(&path, "%s/configuration", path);
EXPECT_WRITE(path, str);
}
void push_config_string(struct test_config *config, int lang, const char *str)
{
char *path;
- int tmp;
-
- tmp = asprintf(&path, "%s/%s/strings/0x%x",
+ safe_asprintf(&path, "%s/%s/strings/0x%x",
config->path, config->name, lang);
- if (tmp < 0)
- fail();
- free_later(path);
EXPECT_OPENDIR(path);
- tmp = asprintf(&path, "%s/configuration", path);
- if (tmp < 0)
- fail();
- free_later(path);
+ safe_asprintf(&path, "%s/configuration", path);
PUSH_FILE(path, str);
}