BUGFIX check return codes of asprintf() and chdir()
For some compilers these functions are defined with warn_unused_result
attribute and ignoring the return value generates compiler's warning
diff --git a/src/context.c b/src/context.c
index f60895f..23b105f 100644
--- a/src/context.c
+++ b/src/context.c
@@ -73,7 +73,10 @@
return NULL;
}
ctx->models.search_path = get_current_dir_name();
- chdir(cwd);
+ if (chdir(cwd)) {
+ LOGWRN("Unable to return back to working directory \"%s\" (%s)",
+ cwd, strerror(errno));
+ }
free(cwd);
}
ctx->models.module_set_id = 1;
@@ -125,7 +128,10 @@
free(ctx->models.search_path);
ctx->models.search_path = get_current_dir_name();
- chdir(cwd);
+ if (chdir(cwd)) {
+ LOGWRN("Unable to return back to working directory \"%s\" (%s)",
+ cwd, strerror(errno));
+ }
free(cwd);
} else {
free(ctx->models.search_path);
@@ -441,7 +447,7 @@
return EXIT_FAILURE;
}
if (cur_mod->inc[i].submodule->filepath) {
- if (asprintf(&str, "file://%s", cur_mod->inc[i].submodule->filepath) < 0) {
+ if (asprintf(&str, "file://%s", cur_mod->inc[i].submodule->filepath) == -1< 0) {
LOGMEM;
return EXIT_FAILURE;
} else if (!lyd_new_leaf(cont, NULL, "schema", str)) {
@@ -492,7 +498,7 @@
return NULL;
}
if (ctx->models.list[i]->filepath) {
- if (asprintf(&str, "file://%s", ctx->models.list[i]->filepath) < 0) {
+ if (asprintf(&str, "file://%s", ctx->models.list[i]->filepath) == -1) {
LOGMEM;
lyd_free(root);
return NULL;
diff --git a/src/parser_json.c b/src/parser_json.c
index 763281b..8231382 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -310,7 +310,10 @@
/* we are done */
if (!start) {
/* XML in string as we print it */
- asprintf(&xmlstr, "<%s xmlns=\"%s\"/>", axml->schema->name, axml->schema->module->ns);
+ if (asprintf(&xmlstr, "<%s xmlns=\"%s\"/>", axml->schema->name, axml->schema->module->ns) == -1) {
+ LOGMEM;
+ return 0;
+ }
axml->value = lyxml_parse_elem(axml->schema->module->ctx, xmlstr, &x, NULL);
free(xmlstr);
diff --git a/src/tree_data.c b/src/tree_data.c
index 7a3f69f..1b580e5 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -352,7 +352,11 @@
ret->prev = (struct lyd_node *)ret;
/* store the anyxml data together with the anyxml element */
- asprintf(&xml, "<%s>%s</%s>", schema->name, (val_xml ? val_xml : ""), schema->name);
+ if (asprintf(&xml, "<%s>%s</%s>", schema->name, (val_xml ? val_xml : ""), schema->name) == -1) {
+ LOGMEM;
+ lyd_free((struct lyd_node *)ret);
+ return NULL;
+ }
root = lyxml_parse_mem(schema->module->ctx, xml, 0);
free(xml);
if (!root) {
diff --git a/src/tree_schema.c b/src/tree_schema.c
index deb6ad2..af6910e 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -1092,11 +1092,12 @@
if (module && !module->filepath) {
/* get URI if there is /proc */
addr = NULL;
- asprintf(&addr, "/proc/self/fd/%d", fd);
- if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
- ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
+ if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
+ if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
+ ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
+ }
+ free(addr);
}
- free(addr);
}
return module;
diff --git a/src/xpath.c b/src/xpath.c
index d8dacdd..6a4799d 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -829,9 +829,13 @@
} else if (isinf(set->value.num) && signbit(set->value.num)) {
str_num = strdup("-Infinity");
} else if ((long long)set->value.num == set->value.num) {
- asprintf(&str_num, "%lld", (long long)set->value.num);
+ if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
+ str_num = NULL;
+ }
} else {
- asprintf(&str_num, "%03.1Lf", set->value.num);
+ if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
+ str_num = NULL;
+ }
}
if (!str_num) {
@@ -6549,9 +6553,13 @@
} else if (isinf(set->value.num) && signbit(set->value.num)) {
str_num = strdup("-Infinity");
} else if ((long long)set->value.num == set->value.num) {
- asprintf(&str_num, "%lld", (long long)set->value.num);
+ if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
+ str_num = NULL;
+ }
} else {
- asprintf(&str_num, "%03.1Lf", set->value.num);
+ if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
+ str_num = NULL;
+ }
}
if (!str_num) {
LOGMEM;
@@ -6632,10 +6640,16 @@
} else if (isinf(set->value.num) && signbit(set->value.num)) {
set->value.str = lydict_insert(ctx, "-Infinity", 0);
} else if ((long long)set->value.num == set->value.num) {
- asprintf(&str_num, "%lld", (long long)set->value.num);
+ if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
+ LOGMEM;
+ return;
+ }
set->value.str = lydict_insert_zc(ctx, str_num);
} else {
- asprintf(&str_num, "%03.1Lf", set->value.num);
+ if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
+ LOGMEM;
+ return;
+ }
set->value.str = lydict_insert_zc(ctx, str_num);
}
break;