summaryrefslogtreecommitdiff
path: root/src/format.c
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2023-02-10 15:27:06 +0100
committerJörg Frings-Fürst <debian@jff-webhosting.net>2023-02-10 15:27:06 +0100
commit7501bff8432444b7ae8e7f3d9289c0d61f3f0b64 (patch)
treebd53603f464c3747e897a8996158a0fef7b41bc3 /src/format.c
parent0f124df68d87c9073f76efeff1a901a69b1f3e13 (diff)
parent9e9336185f86bd97ff22f54e4d561c2cccccecf5 (diff)
Merge branch 'release/debian/4.10-1'debian/4.10-1
Diffstat (limited to 'src/format.c')
-rw-r--r--src/format.c75
1 files changed, 44 insertions, 31 deletions
diff --git a/src/format.c b/src/format.c
index 0c463b0..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,10 +103,13 @@ 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)
{
+ if (blk == NULL)
+ return;
HXmap_free(blk->vars);
HXmap_free(blk->funcs);
free(blk);
@@ -295,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;
@@ -304,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;
}
@@ -641,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;
@@ -677,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;
@@ -688,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)
@@ -703,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;
@@ -713,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;
}