Skip to content

Commit 4c89473

Browse files
jeremie6windmichalvasko
authored andcommitted
common UPDATE allow custom error message
np_reply_err_sr checks the "(path" pattern in the error log to allocate the path variable. When a RPC creates a custom error message without the pattern, the path variable is not allocated. netopeer2-server can crash. In this patch, don't use the path variable if not allocated and produce a default error on cleanup.
1 parent cd353d7 commit 4c89473

1 file changed

Lines changed: 28 additions & 11 deletions

File tree

src/common.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,19 +1520,25 @@ np_err(const struct ly_ctx *ly_ctx, const char *err_msg, int err_code, const cha
15201520

15211521
if (!strncmp(err_msg, "Unique data leaf(s)", 19)) {
15221522
/* data-not-unique */
1523-
assert(err_path);
1523+
if (!err_path) {
1524+
goto cleanup;
1525+
}
15241526
err_info_elem[0] = "non-unique";
15251527
err_info_val[0] = err_path;
15261528
e = np_err_create(ly_ctx, "protocol", "operation-failed", "data-not-unique", NULL,
15271529
"Unique constraint violated.", err_info_elem, err_info_val, 1);
15281530
} else if (!strncmp(err_msg, "Too many", 8)) {
15291531
/* too-many-elements */
1530-
assert(err_path);
1532+
if (!err_path) {
1533+
goto cleanup;
1534+
}
15311535
e = np_err_create(ly_ctx, "protocol", "operation-failed", "too-many-elements", err_path, "Too many elements.",
15321536
NULL, NULL, 0);
15331537
} else if (!strncmp(err_msg, "Too few", 7)) {
15341538
/* too-few-elements */
1535-
assert(err_path);
1539+
if (!err_path) {
1540+
goto cleanup;
1541+
}
15361542
e = np_err_create(ly_ctx, "protocol", "operation-failed", "too-few-elements", err_path, "Too few elements.",
15371543
NULL, NULL, 0);
15381544
} else if (!strncmp(err_msg, "Must condition", 14)) {
@@ -1542,7 +1548,9 @@ np_err(const struct ly_ctx *ly_ctx, const char *err_msg, int err_code, const cha
15421548
str = strndup(err_msg, ptr - err_msg);
15431549

15441550
/* must-violation */
1545-
assert(err_path);
1551+
if (!err_path) {
1552+
goto cleanup;
1553+
}
15461554
e = np_err_create(ly_ctx, "protocol", "operation-failed", "must-violation", err_path, str, NULL, NULL, 0);
15471555
} else if (!strncmp(err_msg, "Invalid leafref value", 21) && strstr(err_msg, "no target instance")) {
15481556
/* get the value */
@@ -1554,7 +1562,9 @@ np_err(const struct ly_ctx *ly_ctx, const char *err_msg, int err_code, const cha
15541562
}
15551563

15561564
/* instance-required */
1557-
assert(err_path);
1565+
if (!err_path) {
1566+
goto cleanup;
1567+
}
15581568
e = np_err_create(ly_ctx, "protocol", "data-missing", "instance-required", err_path, str2, NULL, NULL, 0);
15591569
} else if (!strncmp(err_msg, "Invalid instance-identifier", 26) && strstr(err_msg, "required instance not found")) {
15601570
/* get the value */
@@ -1566,11 +1576,15 @@ np_err(const struct ly_ctx *ly_ctx, const char *err_msg, int err_code, const cha
15661576
}
15671577

15681578
/* instance-required */
1569-
assert(err_path);
1579+
if (!err_path) {
1580+
goto cleanup;
1581+
}
15701582
e = np_err_create(ly_ctx, "protocol", "data-missing", "instance-required", err_path, str2, NULL, NULL, 0);
15711583
} else if (!strncmp(err_msg, "Mandatory choice", 16)) {
15721584
/* get the choice */
1573-
assert(err_path);
1585+
if (!err_path) {
1586+
goto cleanup;
1587+
}
15741588
str = np_err_reply_get_quoted_string(err_msg, 0);
15751589

15761590
/* missing-choice */
@@ -1599,7 +1613,9 @@ np_err(const struct ly_ctx *ly_ctx, const char *err_msg, int err_code, const cha
15991613
}
16001614

16011615
/* get element name */
1602-
assert(err_path);
1616+
if (!err_path) {
1617+
goto cleanup;
1618+
}
16031619
ptr = strrchr(err_path, ':');
16041620
if (!ptr) {
16051621
ptr = strrchr(err_path, '/');
@@ -1681,14 +1697,15 @@ np_err(const struct ly_ctx *ly_ctx, const char *err_msg, int err_code, const cha
16811697
} else if (err_code == SR_ERR_NO_MEMORY) {
16821698
/* resource-denied */
16831699
e = np_err_create(ly_ctx, "application", "resource-denied", NULL, NULL, err_msg, NULL, NULL, 0);
1684-
} else {
1685-
/* generic error */
1686-
e = np_err_create(ly_ctx, "application", "operation-failed", NULL, NULL, err_msg, NULL, NULL, 0);
16871700
}
16881701

16891702
cleanup:
16901703
free(str);
16911704
free(str2);
1705+
if (!e) {
1706+
/* generic error */
1707+
e = np_err_create(ly_ctx, "application", "operation-failed", NULL, NULL, err_msg, NULL, NULL, 0);
1708+
}
16921709
return e;
16931710
}
16941711

0 commit comments

Comments
 (0)