[PATCH 1/3] keytoc: check for memory allocation failures

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



System headers may mark asprintf with an attribute requiring that the
result isn't used unchecked. Add the check and check the malloc() return
code as well for good measure.

Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxx>
Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx>
---
 scripts/keytoc.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/scripts/keytoc.c b/scripts/keytoc.c
index d5c8aa2e1216..c60df8a5f017 100644
--- a/scripts/keytoc.c
+++ b/scripts/keytoc.c
@@ -28,6 +28,12 @@
 
 static int dts, standalone;
 
+static void enomem_exit(const char *func)
+{
+	fprintf(stderr, "%s: Out of memory\n", func);
+	exit(2);
+}
+
 static int openssl_error(const char *fmt, ...)
 {
 	va_list va;
@@ -351,6 +357,8 @@ static int print_bignum(BIGNUM *num, int num_bits, int width)
 	BN_exp(big2_32, big2, big32, ctx); /* B = 2^width */
 
 	arr = malloc(num_bits / width * sizeof(*arr));
+	if (!arr)
+		enomem_exit("malloc");
 
 	for (i = 0; i < num_bits / width; i++) {
 		BN_mod(tmp, num, big2_32, ctx); /* n = N mod B */
@@ -679,7 +687,9 @@ int main(int argc, char *argv[])
 		}
 
 		if (!keyname) {
-			asprintf(&freep, "key_%d", keynum++);
+			ret = asprintf(&freep, "key_%d", keynum++);
+			if (ret < 0)
+				enomem_exit("asprintf");
 			keyname = freep;
 		}
 
-- 
2.39.5





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux