Skip to content

Commit 2b69bd2

Browse files
committed
schema diff, types, parsers REFACTOR bound the remaining string writes
The schema diff tree, the decimal64 / ipv4-prefix / ipv6-prefix canonical value printers, the date/time helpers and lysc_path_until() still built strings with sprintf()/strcpy() into buffers sized by hand. Each buffer is already large enough, but the writes were unbounded. Convert them to snprintf() bounded by the buffer (sizeof or the allocation size). Output is unchanged. The YANG and YIN pattern modifier parsers duplicated the pattern with malloc() + strcpy(), use strdup() there. The YIN revision-date parser copies LY_REV_SIZE bytes, the length lys_check_date() has just verified.
1 parent 3b7bb22 commit 2b69bd2

8 files changed

Lines changed: 43 additions & 43 deletions

File tree

‎src/parser_yang.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*
1212
* https://opensource.org/licenses/BSD-3-Clause
1313
*/
14+
#define _GNU_SOURCE /* strdup */
15+
1416
#include "parser_internal.h"
1517

1618
#include <assert.h>
@@ -2206,9 +2208,8 @@ parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
22062208
}
22072209

22082210
/* replace the value in the dictionary */
2209-
buf = malloc(strlen(restr->arg.str) + 1);
2211+
buf = strdup(restr->arg.str);
22102212
LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
2211-
strcpy(buf, restr->arg.str);
22122213
lysdict_remove(PARSER_CTX(ctx), restr->arg.str);
22132214

22142215
assert(buf[0] == LYSP_RESTR_PATTERN_ACK);

‎src/parser_yin.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,9 +1005,8 @@ yin_parse_modifier(struct lysp_yin_ctx *ctx, const void *parent, const char **pa
10051005
lysdict_remove(ctx->xmlctx->ctx, temp_val);
10061006

10071007
/* allocate new value */
1008-
modified_val = malloc(strlen(*pat) + 1);
1008+
modified_val = strdup(*pat);
10091009
LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
1010-
strcpy(modified_val, *pat);
10111010
lysdict_remove(ctx->xmlctx->ctx, *pat);
10121011

10131012
/* modify the new value */
@@ -1994,7 +1993,7 @@ yin_parse_revision_date(struct lysp_yin_ctx *ctx, char *rev, struct lysp_ext_ins
19941993
LY_CHECK_ERR_RET(lys_check_date(PARSER_CTX(ctx), temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
19951994
lysdict_remove(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
19961995

1997-
strcpy(rev, temp_rev);
1996+
memcpy(rev, temp_rev, LY_REV_SIZE);
19981997
lysdict_remove(ctx->xmlctx->ctx, temp_rev);
19991998

20001999
LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), rev, LY_STMT_REVISION_DATE, NULL, exts));

‎src/plugins_types/decimal64.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ decimal64_num2str(int64_t num, struct lysc_type_dec *type, char **str)
5353
LY_CHECK_RET(!ret, LY_EMEM);
5454

5555
if (num) {
56-
int count = sprintf(ret, "%" PRId64 " ", num);
56+
int count = snprintf(ret, LY_NUMBER_MAXLEN, "%" PRId64 " ", num);
5757

5858
if (((num > 0) && ((count - 1) <= type->fraction_digits)) || ((count - 2) <= type->fraction_digits)) {
5959
/* we have 0. value, print the value with the leading zeros
6060
* (one for 0. and also keep the correct with of num according
6161
* to fraction-digits value)
6262
* for (num < 0) - extra character for '-' sign */
63-
count = sprintf(ret, "%0*" PRId64 " ", (num > 0) ? (type->fraction_digits + 1) : (type->fraction_digits + 2), num);
63+
count = snprintf(ret, LY_NUMBER_MAXLEN, "%0*" PRId64 " ", (num > 0) ? (type->fraction_digits + 1) : (type->fraction_digits + 2), num);
6464
}
6565
for (uint8_t i = type->fraction_digits, j = 1; i > 0; i--) {
6666
if (j && (i > 1) && (ret[count - 2] == '0')) {
@@ -75,7 +75,7 @@ decimal64_num2str(int64_t num, struct lysc_type_dec *type, char **str)
7575
ret[count - 1] = '.';
7676
} else {
7777
/* zero */
78-
sprintf(ret, "0.0");
78+
snprintf(ret, LY_NUMBER_MAXLEN, "0.0");
7979
}
8080

8181
*str = ret;

‎src/plugins_types/ipv4_address_prefix.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ lyplg_type_print_ipv4_address_prefix(const struct ly_ctx *ctx, const struct lyd_
275275
}
276276

277277
/* add the prefix */
278-
sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
278+
snprintf(ret + strlen(ret), INET_ADDRSTRLEN + 4 - strlen(ret), "/%" PRIu8, val->prefix);
279279

280280
/* store it */
281281
if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {

‎src/plugins_types/ipv6_address_prefix.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ lyplg_type_print_ipv6_address_prefix(const struct ly_ctx *ctx, const struct lyd_
289289
}
290290

291291
/* add the prefix */
292-
sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
292+
snprintf(ret + strlen(ret), INET6_ADDRSTRLEN + 4 - strlen(ret), "/%" PRIu8, val->prefix);
293293

294294
/* store it */
295295
if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {

‎src/schema_diff_tree.c‎

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ schema_diff_pnode_path(const struct lysp_node *node)
316316
path = ly_realloc(path, new_len + 1);
317317
LY_CHECK_ERR_GOTO(!path, LOGMEM(NULL), cleanup);
318318

319-
sprintf(path + path_len, "/%s%s%s", node_pre ? node_pre : "", nodes[i]->name, node_post ? node_post : "");
319+
snprintf(path + path_len, new_len + 1 - path_len, "/%s%s%s", node_pre ? node_pre : "", nodes[i]->name, node_post ? node_post : "");
320320
path_len = new_len;
321321
} while (i);
322322

@@ -491,11 +491,11 @@ schema_diff_ext_inst_substmts_bitenums(const struct ly_ctx *ctx, const struct ly
491491

492492
/* value/position */
493493
if (items[u].flags & LYS_IS_ENUM) {
494-
sprintf(num_str, "%" PRId32, items[u].value);
494+
snprintf(num_str, sizeof num_str, "%" PRId32, items[u].value);
495495
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, "value", num_str, &bitenum_node->child),
496496
cleanup);
497497
} else {
498-
sprintf(num_str, "%" PRIu32, items[u].position);
498+
snprintf(num_str, sizeof num_str, "%" PRIu32, items[u].position);
499499
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, "position", num_str, &bitenum_node->child),
500500
cleanup);
501501
}
@@ -611,16 +611,16 @@ schema_diff_range2str(struct lysc_range_part *parts, ly_bool is_signed, char **s
611611

612612
LYA_FOR(parts, u) {
613613
if (is_signed) {
614-
sprintf(num_min_str, "%" PRId64, parts[u].min_64);
614+
snprintf(num_min_str, sizeof num_min_str, "%" PRId64, parts[u].min_64);
615615
if (parts[u].min_64 < parts[u].max_64) {
616-
sprintf(num_max_str, "%" PRId64, parts[u].max_64);
616+
snprintf(num_max_str, sizeof num_max_str, "%" PRId64, parts[u].max_64);
617617
} else {
618618
num_max_str[0] = '\0';
619619
}
620620
} else {
621-
sprintf(num_min_str, "%" PRIu64, parts[u].min_u64);
621+
snprintf(num_min_str, sizeof num_min_str, "%" PRIu64, parts[u].min_u64);
622622
if (parts[u].min_u64 < parts[u].max_u64) {
623-
sprintf(num_max_str, "%" PRIu64, parts[u].max_u64);
623+
snprintf(num_max_str, sizeof num_max_str, "%" PRIu64, parts[u].max_u64);
624624
} else {
625625
num_max_str[0] = '\0';
626626
}
@@ -630,7 +630,7 @@ schema_diff_range2str(struct lysc_range_part *parts, ly_bool is_signed, char **s
630630
s = ly_realloc(s, new_len + 1);
631631
LY_CHECK_ERR_GOTO(!s, LOGMEM(NULL); rc = LY_EMEM, cleanup);
632632

633-
sprintf(s + s_len, "%s%s%s%s", s_len ? "|" : "", num_min_str, num_max_str[0] ? ".." : "",
633+
snprintf(s + s_len, new_len + 1 - s_len, "%s%s%s%s", s_len ? "|" : "", num_min_str, num_max_str[0] ? ".." : "",
634634
num_max_str[0] ? num_max_str : "");
635635
s_len = new_len;
636636
}
@@ -877,7 +877,7 @@ schema_diff_ext_inst_substmts_type(const struct ly_ctx *ctx, const struct lysc_t
877877
type_dec = (const struct lysc_type_dec *)type;
878878

879879
/* fraction-digits */
880-
sprintf(num_str, "%" PRIu8, type_dec->fraction_digits);
880+
snprintf(num_str, sizeof num_str, "%" PRIu8, type_dec->fraction_digits);
881881
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, "fraction-digits", num_str, child_p), cleanup);
882882

883883
/* range */
@@ -1099,12 +1099,12 @@ schema_diff_ext_inst_substmts_nodes_r(const struct lysc_node *sibling, struct ly
10991099
max = llist->max;
11001100
}
11011101
if (min > 0) {
1102-
sprintf(num_str, "%" PRIu32, min);
1102+
snprintf(num_str, sizeof num_str, "%" PRIu32, min);
11031103
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, "min-elements", num_str, &node->child),
11041104
cleanup);
11051105
}
11061106
if (max < UINT32_MAX) {
1107-
sprintf(num_str, "%" PRIu32, max);
1107+
snprintf(num_str, sizeof num_str, "%" PRIu32, max);
11081108
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, "max-elements", num_str, &node->child),
11091109
cleanup);
11101110
}
@@ -1221,7 +1221,7 @@ schema_diff_ext_inst_substmts(const struct ly_ctx *ctx, const struct lysc_ext_su
12211221

12221222
/* uint8 number */
12231223
if (*(substmts[u].storage_p)) {
1224-
sprintf(num_str, "%" PRIu8, *(uint8_t *)substmts[u].storage_p);
1224+
snprintf(num_str, sizeof num_str, "%" PRIu8, *(uint8_t *)substmts[u].storage_p);
12251225
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, lys_stmt_str(substmts[u].stmt), num_str,
12261226
child_p), cleanup);
12271227
}
@@ -1253,7 +1253,7 @@ schema_diff_ext_inst_substmts(const struct ly_ctx *ctx, const struct lysc_ext_su
12531253

12541254
/* uint32 number */
12551255
if (*(uint32_t *)substmts[u].storage_p < UINT32_MAX) {
1256-
sprintf(num_str, "%" PRIu32, *(uint32_t *)substmts[u].storage_p);
1256+
snprintf(num_str, sizeof num_str, "%" PRIu32, *(uint32_t *)substmts[u].storage_p);
12571257
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, "max-elements", num_str, child_p),
12581258
cleanup);
12591259
}
@@ -1264,7 +1264,7 @@ schema_diff_ext_inst_substmts(const struct ly_ctx *ctx, const struct lysc_ext_su
12641264

12651265
/* uint32 number */
12661266
if (*(uint32_t *)substmts[u].storage_p > 0) {
1267-
sprintf(num_str, "%" PRIu32, *(uint32_t *)substmts[u].storage_p);
1267+
snprintf(num_str, sizeof num_str, "%" PRIu32, *(uint32_t *)substmts[u].storage_p);
12681268
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, "min-elements", num_str, child_p),
12691269
cleanup);
12701270
}
@@ -1290,7 +1290,7 @@ schema_diff_ext_inst_substmts(const struct ly_ctx *ctx, const struct lysc_ext_su
12901290
char num_str[21];
12911291

12921292
/* uint64 number */
1293-
sprintf(num_str, "%" PRIu64, *(uint64_t *)substmts[u].storage_p);
1293+
snprintf(num_str, sizeof num_str, "%" PRIu64, *(uint64_t *)substmts[u].storage_p);
12941294
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, "position", num_str, child_p), cleanup);
12951295
break;
12961296
}
@@ -1334,7 +1334,7 @@ schema_diff_ext_inst_substmts(const struct ly_ctx *ctx, const struct lysc_ext_su
13341334
char num_str[21];
13351335

13361336
/* int64 number */
1337-
sprintf(num_str, "%" PRId64, *(int64_t *)substmts[u].storage_p);
1337+
snprintf(num_str, sizeof num_str, "%" PRId64, *(int64_t *)substmts[u].storage_p);
13381338
LY_CHECK_GOTO(rc = schema_diff_ext_inst_substmts_child_add(ctx, "value", num_str, child_p), cleanup);
13391339
break;
13401340
}
@@ -1958,11 +1958,11 @@ schema_diff_parsed_enum(const struct lysp_type_enum *enm, ly_bool is_bit, struct
19581958

19591959
if (is_bit) {
19601960
/* position */
1961-
sprintf(str, "%" PRIu64, (uint64_t)enm->value);
1961+
snprintf(str, sizeof str, "%" PRIu64, (uint64_t)enm->value);
19621962
LY_CHECK_GOTO(rc = lyd_new_term(enum_list, NULL, "position", str, LYD_NEW_VAL_CANON, NULL), cleanup);
19631963
} else {
19641964
/* value */
1965-
sprintf(str, "%" PRId64, enm->value);
1965+
snprintf(str, sizeof str, "%" PRId64, enm->value);
19661966
LY_CHECK_GOTO(rc = lyd_new_term(enum_list, NULL, "value", str, LYD_NEW_VAL_CANON, NULL), cleanup);
19671967
}
19681968

@@ -2011,7 +2011,7 @@ schema_diff_parsed_type(const struct lysp_type *type, struct lyd_node *cont)
20112011

20122012
/* fraction-digits */
20132013
if (type->flags & LYS_SET_FRDIGITS) {
2014-
sprintf(str, "%" PRIu8, type->fraction_digits);
2014+
snprintf(str, sizeof str, "%" PRIu8, type->fraction_digits);
20152015
LY_CHECK_GOTO(rc = lyd_new_term(cont, NULL, "fraction-digits", str, LYD_NEW_VAL_CANON, NULL), cleanup);
20162016
}
20172017

@@ -2264,15 +2264,15 @@ schema_diff_pnode(const struct lysp_node *pnode, struct lyd_node *change_cont)
22642264

22652265
/* min-elements */
22662266
if (min_set) {
2267-
sprintf(buf, "%" PRIu32, min);
2267+
snprintf(buf, sizeof buf, "%" PRIu32, min);
22682268
if ((rc = lyd_new_term(change_cont, NULL, "min-elements", buf, 0, NULL))) {
22692269
goto cleanup;
22702270
}
22712271
}
22722272

22732273
/* max-elements */
22742274
if (max_set) {
2275-
sprintf(buf, "%" PRIu32, max);
2275+
snprintf(buf, sizeof buf, "%" PRIu32, max);
22762276
if ((rc = lyd_new_term(change_cont, NULL, "max-elements", buf, 0, NULL))) {
22772277
goto cleanup;
22782278
}
@@ -2405,15 +2405,15 @@ schema_diff_refine(const struct lysp_refine *refine, struct lyd_node *change_con
24052405

24062406
/* min-elements */
24072407
if (refine->flags & LYS_SET_MIN) {
2408-
sprintf(buf, "%" PRIu32, refine->min);
2408+
snprintf(buf, sizeof buf, "%" PRIu32, refine->min);
24092409
if ((rc = lyd_new_term(change_cont, NULL, "min-elements", buf, 0, NULL))) {
24102410
goto cleanup;
24112411
}
24122412
}
24132413

24142414
/* max-elements */
24152415
if (refine->flags & LYS_SET_MAX) {
2416-
sprintf(buf, "%" PRIu32, refine->max);
2416+
snprintf(buf, sizeof buf, "%" PRIu32, refine->max);
24172417
if ((rc = lyd_new_term(change_cont, NULL, "max-elements", buf, 0, NULL))) {
24182418
goto cleanup;
24192419
}
@@ -3122,15 +3122,15 @@ schema_diff_deviation(const struct lysp_deviation *dev, struct lyd_node *change_
31223122

31233123
/* min-elements */
31243124
if (flags & LYS_SET_MIN) {
3125-
sprintf(buf, "%" PRIu32, min);
3125+
snprintf(buf, sizeof buf, "%" PRIu32, min);
31263126
if ((rc = lyd_new_term(dev_list, NULL, "min-elements", buf, 0, NULL))) {
31273127
goto cleanup;
31283128
}
31293129
}
31303130

31313131
/* max-elements */
31323132
if (flags & LYS_SET_MAX) {
3133-
sprintf(buf, "%" PRIu32, max);
3133+
snprintf(buf, sizeof buf, "%" PRIu32, max);
31343134
if ((rc = lyd_new_term(dev_list, NULL, "max-elements", buf, 0, NULL))) {
31353135
goto cleanup;
31363136
}
@@ -3523,10 +3523,10 @@ schema_diff_node_type_bitenums(const struct lysc_type_bitenum_item *items, struc
35233523

35243524
/* value/position */
35253525
if (items[u].flags & LYS_IS_ENUM) {
3526-
sprintf(buf, "%" PRId32, items[u].value);
3526+
snprintf(buf, sizeof buf, "%" PRId32, items[u].value);
35273527
LY_CHECK_GOTO(rc = lyd_new_term(par_list, NULL, "value", buf, 0, NULL), cleanup);
35283528
} else {
3529-
sprintf(buf, "%" PRIu32, items[u].position);
3529+
snprintf(buf, sizeof buf, "%" PRIu32, items[u].position);
35303530
LY_CHECK_GOTO(rc = lyd_new_term(par_list, NULL, "position", buf, 0, NULL), cleanup);
35313531
}
35323532

@@ -3625,7 +3625,7 @@ schema_diff_node_type(const struct lysc_type *type, struct lyd_node *type_par)
36253625
type_dec = (const struct lysc_type_dec *)type;
36263626

36273627
/* fraction-digits */
3628-
sprintf(buf, "%" PRIu8, type_dec->fraction_digits);
3628+
snprintf(buf, sizeof buf, "%" PRIu8, type_dec->fraction_digits);
36293629
LY_CHECK_GOTO(rc = lyd_new_term(type_par, NULL, "fraction-digits", buf, 0, NULL), cleanup);
36303630

36313631
/* range */
@@ -3828,13 +3828,13 @@ schema_diff_node_stmts(const struct lysc_node *node, ly_bool gen_local, struct l
38283828
max = llist->max;
38293829
}
38303830
if (min > 0) {
3831-
sprintf(buf, "%" PRIu32, min);
3831+
snprintf(buf, sizeof buf, "%" PRIu32, min);
38323832
if ((rc = lyd_new_term(change_cont, NULL, "min-elements", buf, 0, NULL))) {
38333833
goto cleanup;
38343834
}
38353835
}
38363836
if (max < UINT32_MAX) {
3837-
sprintf(buf, "%" PRIu32, max);
3837+
snprintf(buf, sizeof buf, "%" PRIu32, max);
38383838
if ((rc = lyd_new_term(change_cont, NULL, "max-elements", buf, 0, NULL))) {
38393839
goto cleanup;
38403840
}

‎src/tree_data_common.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ ly_time_time2str(time_t time, const char *fractions_s, char **str)
16481648
zonediff_s = ly_time_tz_offset_at(time);
16491649
zonediff_h = zonediff_s / 60 / 60;
16501650
zonediff_m = zonediff_s / 60 % 60;
1651-
sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m < 0 ? -zonediff_m : zonediff_m);
1651+
snprintf(zoneshift, sizeof zoneshift, "%+03d:%02d", zonediff_h, zonediff_m < 0 ? -zonediff_m : zonediff_m);
16521652

16531653
/* print */
16541654
if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
@@ -1698,7 +1698,7 @@ ly_time_ts2str(const struct timespec *ts, char **str)
16981698

16991699
/* convert nanoseconds to fractions of a second */
17001700
if (ts->tv_nsec) {
1701-
sprintf(frac_buf, "%09ld", ts->tv_nsec);
1701+
snprintf(frac_buf, sizeof frac_buf, "%09ld", ts->tv_nsec);
17021702
}
17031703

17041704
return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str);

‎src/tree_schema.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LY
10001000
path = NULL;
10011001
} else if (len == 0) {
10021002
if (buffer) {
1003-
strcpy(buffer, "/");
1003+
snprintf(buffer, buflen, "/");
10041004
} else {
10051005
free(path);
10061006
path = strdup("/");

0 commit comments

Comments
 (0)