Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 28 Apr 2020 20:34:27 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r360452 - head/bin/sh
Message-ID:  <202004282034.03SKYRNb079944@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Tue Apr 28 20:34:27 2020
New Revision: 360452
URL: https://svnweb.freebsd.org/changeset/base/360452

Log:
  sh: Assert INTOFF rather than applying it in ck*
  
  As I noted in https://reviews.freebsd.org/D22756, INTOFF should be in effect
  when calling ckmalloc/ckrealloc/ckfree to avoid memory leaks and double
  frees. Therefore, change the functions to check if INTOFF is in effect
  instead of applying it.
  
  Reviewed by:	bdrewery
  Differential Revision:	https://reviews.freebsd.org/D24599

Modified:
  head/bin/sh/memalloc.c

Modified: head/bin/sh/memalloc.c
==============================================================================
--- head/bin/sh/memalloc.c	Tue Apr 28 20:14:38 2020	(r360451)
+++ head/bin/sh/memalloc.c	Tue Apr 28 20:34:27 2020	(r360452)
@@ -50,6 +50,13 @@ __FBSDID("$FreeBSD$");
 #include <stdlib.h>
 #include <unistd.h>
 
+static void
+badalloc(const char *message)
+{
+	write(2, message, strlen(message));
+	abort();
+}
+
 /*
  * Like malloc, but returns an error when out of space.
  */
@@ -59,9 +66,9 @@ ckmalloc(size_t nbytes)
 {
 	pointer p;
 
-	INTOFF;
+	if (!is_int_on())
+		badalloc("Unsafe ckmalloc() call\n");
 	p = malloc(nbytes);
-	INTON;
 	if (p == NULL)
 		error("Out of space");
 	return p;
@@ -75,9 +82,9 @@ ckmalloc(size_t nbytes)
 pointer
 ckrealloc(pointer p, int nbytes)
 {
-	INTOFF;
+	if (!is_int_on())
+		badalloc("Unsafe ckrealloc() call\n");
 	p = realloc(p, nbytes);
-	INTON;
 	if (p == NULL)
 		error("Out of space");
 	return p;
@@ -86,9 +93,9 @@ ckrealloc(pointer p, int nbytes)
 void
 ckfree(pointer p)
 {
-	INTOFF;
+	if (!is_int_on())
+		badalloc("Unsafe ckfree() call\n");
 	free(p);
-	INTON;
 }
 
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202004282034.03SKYRNb079944>