[LTP] [PATCH v2 09/15] ujson: writer: Check return value in esc_flush()
Andrea Cervesato
andrea.cervesato@suse.de
Tue Oct 13 14:02:05 CEST 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
ESC_FLUSH ignored the return values of out() and out_str(). If an
output error occurred while flushing escaped characters, the error was
ignored and writing continued. Replace ESC_FLUSH with an esc_flush()
helper function, check its return value, and propagate errors.
Fixes: c50a0e0751c8 ("libs: Vendor ujson library")
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
libs/ujson/ujson_writer.c | 37 +++++++++++++++++++++++--------------
1 file changed, 23 insertions(+), 14 deletions(-)
diff --git a/libs/ujson/ujson_writer.c b/libs/ujson/ujson_writer.c
index c203ee079..7882a5ab5 100644
--- a/libs/ujson/ujson_writer.c
+++ b/libs/ujson/ujson_writer.c
@@ -97,12 +97,16 @@ static inline int out_ch(ujson_writer *self, char ch)
return out(self, &ch, 1);
}
-#define ESC_FLUSH(esc_char) do {\
- out(self, val, i); \
- val += i + 1; \
- i = 0; \
- out_str(self, esc_char); \
-} while (0)
+static inline int esc_flush(ujson_writer *self, const char **val, size_t *i, const char *esc_char)
+{
+ if (out(self, *val, *i))
+ return 1;
+
+ *val += *i + 1;
+ *i = 0;
+
+ return out_str(self, esc_char);
+}
static inline int out_esc_str(ujson_writer *self, const char *val)
{
@@ -120,34 +124,39 @@ static inline int out_esc_str(ujson_writer *self, const char *val)
}
if (next_chsz == 1) {
+ int ret = 0;
+
switch (val[i]) {
case '\"':
- ESC_FLUSH("\\\"");
+ ret = esc_flush(self, &val, &i, "\\\"");
break;
case '\\':
- ESC_FLUSH("\\\\");
+ ret = esc_flush(self, &val, &i, "\\\\");
break;
case '/':
- ESC_FLUSH("\\/");
+ ret = esc_flush(self, &val, &i, "\\/");
break;
case '\b':
- ESC_FLUSH("\\b");
+ ret = esc_flush(self, &val, &i, "\\b");
break;
case '\f':
- ESC_FLUSH("\\f");
+ ret = esc_flush(self, &val, &i, "\\f");
break;
case '\n':
- ESC_FLUSH("\\n");
+ ret = esc_flush(self, &val, &i, "\\n");
break;
case '\r':
- ESC_FLUSH("\\r");
+ ret = esc_flush(self, &val, &i, "\\r");
break;
case '\t':
- ESC_FLUSH("\\t");
+ ret = esc_flush(self, &val, &i, "\\t");
break;
default:
i += next_chsz;
}
+
+ if (ret)
+ return 1;
} else {
i += next_chsz;
}
--
2.51.0
More information about the ltp
mailing list