diff options
Diffstat (limited to 'src/format.c')
-rw-r--r-- | src/format.c | 73 |
1 files changed, 42 insertions, 31 deletions
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 <unistd.h> #include <libHX.h> #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; } @@ -643,6 +632,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; int ret = 0; @@ -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; @@ -690,10 +687,17 @@ 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) @@ -705,8 +709,15 @@ 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; } |