json-c/backport-Handle-yet-another-out-of-memory-condition.patch
2025-03-19 11:32:18 +08:00

65 lines
1.7 KiB
Diff

From 833233faa8d6835276ebbd48b92c7feeb141270d Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Mon, 22 Apr 2024 01:50:59 +0200
Subject: [PATCH] Handle yet another out-of-memory condition.
duplocale() can return NULL, with errno set to ENOMEM.
In this case, bail out and set the current error code to
json_tokener_error_memory.
---
json_tokener.c | 9 ++++++++-
json_tokener.h | 3 ++-
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/json_tokener.c b/json_tokener.c
index 6527270..4a2e01e 100644
--- a/json_tokener.c
+++ b/json_tokener.c
@@ -17,6 +17,7 @@
#include "math_compat.h"
#include <assert.h>
+#include <errno.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
@@ -87,7 +88,8 @@ static const char *json_tokener_errors[] = {
"invalid string sequence",
"expected comment",
"invalid utf-8 string",
- "buffer size overflow"
+ "buffer size overflow",
+ "out of memory"
};
/* clang-format on */
@@ -289,6 +291,11 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
#ifdef HAVE_USELOCALE
{
locale_t duploc = duplocale(oldlocale);
+ if (duploc == NULL && errno == ENOMEM)
+ {
+ tok->err = json_tokener_error_memory;
+ return NULL;
+ }
newloc = newlocale(LC_NUMERIC_MASK, "C", duploc);
if (newloc == NULL)
{
diff --git a/json_tokener.h b/json_tokener.h
index a07e12c..ce412e2 100644
--- a/json_tokener.h
+++ b/json_tokener.h
@@ -40,7 +40,8 @@ enum json_tokener_error
json_tokener_error_parse_string,
json_tokener_error_parse_comment,
json_tokener_error_parse_utf8_string,
- json_tokener_error_size
+ json_tokener_error_size,
+ json_tokener_error_memory
};
/**
--
2.43.0