From 109a9e7bad007f962dd9695f11b83039db58381e Mon Sep 17 00:00:00 2001 From: Daniel Nussenbaum Date: Tue, 17 Sep 2024 18:36:58 +0300 Subject: [PATCH 1/2] Solution to Memory leaks by interning external strings #24 per @sletner --- headers.lisp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/headers.lisp b/headers.lisp index 06792ed..94a5067 100644 --- a/headers.lisp +++ b/headers.lisp @@ -26,6 +26,22 @@ ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +;;; The following code is for treating headers which exist in the keyword package as keywords, otherwise they will be treated as strings. This code will override the chunga code which by default interns every header to be a keyword + +(in-package :chunga) + +(defvar *intern-unsafely*) + +(defun as-keyword (string &key (destructivep t)) + "Checks if the string STRING is found as a keyword and if it is, returns the keyword, otherwise it returns a string. Note that this is obviously not congruent to the name of the function, it is meant to be an override to the default behavior to avoid memory leaks in Hunchentoot" + (or (gethash string +string-to-keyword-hash+) + (find-symbol (string-upcase string) (find-package "KEYWORD")) + (if (and (boundp '*intern-unsafely*) *intern-unsafely*) + (make-keyword string destructivep) + string))) + +;;; End of Headers as Keywords Representation Code + (in-package :hunchentoot) (defgeneric write-header-line (key value stream) From a815c86d746e7e86c6c2d387e25f72d9fdaeb898 Mon Sep 17 00:00:00 2001 From: Daniel Nussenbaum Date: Wed, 18 Sep 2024 15:14:59 +0300 Subject: [PATCH 2/2] adding suggested changes to simplify code --- headers.lisp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/headers.lisp b/headers.lisp index 94a5067..d5e9034 100644 --- a/headers.lisp +++ b/headers.lisp @@ -30,13 +30,12 @@ (in-package :chunga) -(defvar *intern-unsafely*) +(defvar *intern-unsafely* NIL) (defun as-keyword (string &key (destructivep t)) "Checks if the string STRING is found as a keyword and if it is, returns the keyword, otherwise it returns a string. Note that this is obviously not congruent to the name of the function, it is meant to be an override to the default behavior to avoid memory leaks in Hunchentoot" - (or (gethash string +string-to-keyword-hash+) - (find-symbol (string-upcase string) (find-package "KEYWORD")) - (if (and (boundp '*intern-unsafely*) *intern-unsafely*) + (or (find-symbol (string-upcase string) (find-package "KEYWORD")) + (if *intern-unsafely* (make-keyword string destructivep) string)))