Skip to content

Commit 682d7ff

Browse files
committed
schema compile REFACTOR proper error checking when generating path
Refs #2386
1 parent 116f06d commit 682d7ff

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

src/schema_compile_node.c

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4139,49 +4139,82 @@ lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lys
41394139
return rc;
41404140
}
41414141

4142+
/**
4143+
* @brief Generate path of a grouping for logging.
4144+
*
4145+
* @param[in] ctx Compile context.
4146+
* @param[in] node Grouping node.
4147+
* @param[out] path Generated path.
4148+
* @return Length of the generated @p path;
4149+
* @return -1 on error.
4150+
*/
41424151
static int
41434152
lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
41444153
{
41454154
struct lysp_node *iter;
4146-
int len = 0;
4155+
int len = 0, r;
4156+
char *s, *id;
41474157

41484158
*path = NULL;
4149-
for (iter = node; iter && len >= 0; iter = iter->parent) {
4150-
char *s = *path;
4151-
char *id;
41524159

4160+
for (iter = node; iter && (len >= 0); iter = iter->parent) {
4161+
s = *path;
4162+
4163+
/* next node segment */
41534164
switch (iter->nodetype) {
41544165
case LYS_USES:
4155-
LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
4166+
r = asprintf(&id, "{uses='%s'}", iter->name);
41564167
break;
41574168
case LYS_GROUPING:
4158-
LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
4169+
r = asprintf(&id, "{grouping='%s'}", iter->name);
41594170
break;
41604171
case LYS_AUGMENT:
4161-
LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
4172+
r = asprintf(&id, "{augment='%s'}", iter->name);
41624173
break;
41634174
default:
41644175
id = strdup(iter->name);
4176+
r = id ? 1 : -1;
41654177
break;
41664178
}
4179+
if (r == -1) {
4180+
len = -1;
4181+
goto cleanup;
4182+
}
41674183

4184+
/* append the segment to the path */
41684185
if (!iter->parent) {
41694186
/* print prefix */
4170-
len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
4187+
r = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
41714188
} else {
41724189
/* prefix is the same as in parent */
4173-
len = asprintf(path, "/%s%s", id, s ? s : "");
4190+
r = asprintf(path, "/%s%s", id, s ? s : "");
41744191
}
41754192
free(s);
41764193
free(id);
4194+
if (r == -1) {
4195+
len = -1;
4196+
goto cleanup;
4197+
}
4198+
4199+
/* remember the length of the full path */
4200+
len = r;
41774201
}
41784202

4203+
if (!len) {
4204+
/* root node path */
4205+
*path = strdup("/");
4206+
if (!*path) {
4207+
len = -1;
4208+
goto cleanup;
4209+
}
4210+
4211+
len = 1;
4212+
}
4213+
4214+
cleanup:
41794215
if (len < 0) {
41804216
free(*path);
41814217
*path = NULL;
4182-
} else if (len == 0) {
4183-
*path = strdup("/");
4184-
len = 1;
41854218
}
41864219
return len;
41874220
}

0 commit comments

Comments
 (0)