From bfef0924f58eab930bdd826ac0132786abc32220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sun, 29 Jan 2023 16:17:51 +0100 Subject: New upstream version 4.10 --- src/format.c | 73 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 31 deletions(-) (limited to 'src/format.c') diff --git a/src/format.c b/src/format.c index b691dc7..25c09d6 100644 --- a/src/format.c +++ b/src/format.c @@ -16,6 +16,12 @@ #include #include #include "internal.h" +#undef HXformat_aprintf +#undef HXformat_fprintf +#undef HXformat_sprintf +extern int HXformat_aprintf(const struct HXformat_map *, hxmc_t **, const char *); +extern int HXformat_sprintf(const struct HXformat_map *, char *, size_t, const char *); +extern int HXformat_fprintf(const struct HXformat_map *, FILE *, const char *); /* To make it easier on the highlighter */ #define C_OPEN '(' @@ -97,6 +103,7 @@ static void *func_entry_clone(const void *data, size_t size) static const struct HXmap_ops func_entry_ops = { .d_clone = func_entry_clone, + .d_free = free, }; EXPORT_SYMBOL void HXformat_free(struct HXformat_map *blk) @@ -297,7 +304,6 @@ static hxmc_t *HXformat2_snl(int argc, const hxmc_t *const *argv, static hxmc_t *HXformat2_substr(int argc, const hxmc_t *const *argv, const struct HXformat_map *blk) { - ssize_t offset, length, z; hxmc_t *ret; char *end; @@ -306,44 +312,27 @@ static hxmc_t *HXformat2_substr(int argc, const hxmc_t *const *argv, return &HXformat2_nexp; } - offset = strtoll(argv[1], &end, 0); + long w = LONG_MAX, v = strtol(argv[1], &end, 0); if (*end != '\0') { fprintf(stderr, "HXformat2-substr: found garbage in " "offset specification\n"); return &HXformat2_nexp; } - - z = strlen(argv[0]); - if (offset < 0) - offset = z + offset; - if (offset >= z) - return &HXformat2_nexp; - - if (argc == 2) { - if (offset < 0) - offset = 0; - length = z - offset; - } else { - length = strtoll(argv[2], &end, 0); + if (argc >= 3) { + w = strtol(argv[2], &end, 0); if (*end != '\0') { fprintf(stderr, "HXformat2-substr; found garbage in " "length specification\n"); return &HXformat2_nexp; } - if (length < 0) - length/*end*/ = z + length; - else - length/*end*/ = offset + length; - if (offset < 0) - offset = 0; } - if (length <= 0) + size_t start = 0, tocopy = HX_substr_helper(strlen(argv[0]), v, w, &start); + if (tocopy == 0) return &HXformat2_nexp; - - ret = HXmc_meminit(NULL, length); + ret = HXmc_meminit(NULL, tocopy); if (ret == NULL) return &HXformat2_nexp; - if (HXmc_memcpy(&ret, &argv[0][offset], length) == NULL) { + if (HXmc_memcpy(&ret, &argv[0][start], tocopy) == NULL) { HXmc_free(ret); return &HXformat2_nexp; } @@ -642,6 +631,13 @@ EXPORT_SYMBOL struct HXformat_map *HXformat_init(void) EXPORT_SYMBOL int HXformat_aprintf(const struct HXformat_map *blk, hxmc_t **resultp, const char *fmt) +{ + ssize_t ret = HXformat3_aprintf(blk, resultp, fmt); + return ret > INT_MAX ? INT_MAX : ret; +} + +EXPORT_SYMBOL ssize_t HXformat3_aprintf(const struct HXformat_map *blk, + hxmc_t **resultp, const char *fmt) { hxmc_t *ex, *ts, *out; const char *current; @@ -679,7 +675,8 @@ EXPORT_SYMBOL int HXformat_aprintf(const struct HXformat_map *blk, } *resultp = out; - return HXmc_length(out); + size_t xl = HXmc_length(out); + return xl > SSIZE_MAX ? SSIZE_MAX : xl; out: ret = -errno; @@ -689,11 +686,18 @@ EXPORT_SYMBOL int HXformat_aprintf(const struct HXformat_map *blk, EXPORT_SYMBOL int HXformat_fprintf(const struct HXformat_map *ftable, FILE *filp, const char *fmt) +{ + ssize_t ret = HXformat3_fprintf(ftable, filp, fmt); + return ret > INT_MAX ? INT_MAX : ret; +} + +EXPORT_SYMBOL ssize_t HXformat3_fprintf(const struct HXformat_map *ftable, + FILE *filp, const char *fmt) { hxmc_t *str; - int ret; + ssize_t ret; - if ((ret = HXformat_aprintf(ftable, &str, fmt)) <= 0) + if ((ret = HXformat3_aprintf(ftable, &str, fmt)) <= 0) return ret; errno = 0; if (fputs(str, filp) < 0) @@ -704,9 +708,16 @@ EXPORT_SYMBOL int HXformat_fprintf(const struct HXformat_map *ftable, EXPORT_SYMBOL int HXformat_sprintf(const struct HXformat_map *ftable, char *dest, size_t size, const char *fmt) +{ + ssize_t ret = HXformat3_sprintf(ftable, dest, size, fmt); + return ret > INT_MAX ? INT_MAX : ret; +} + +EXPORT_SYMBOL ssize_t HXformat3_sprintf(const struct HXformat_map *ftable, + char *dest, size_t size, const char *fmt) { hxmc_t *str; - int ret; + ssize_t ret; if ((ret = HXformat_aprintf(ftable, &str, fmt)) < 0) return ret; @@ -715,7 +726,7 @@ EXPORT_SYMBOL int HXformat_sprintf(const struct HXformat_map *ftable, return 0; } strncpy(dest, str, size); - ret = HXmc_length(dest); + size_t xl = strlen(dest); HXmc_free(str); - return ret; + return xl > SSIZE_MAX ? SSIZE_MAX : xl; } -- cgit v1.2.3