poke-devel
[Top][All Lists]
Advanced

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

[PATCH v2 1/2] pkl: replace `xzalloc' with `calloc' for `pkl_env'


From: Mohammad-Reza Nabipoor
Subject: [PATCH v2 1/2] pkl: replace `xzalloc' with `calloc' for `pkl_env'
Date: Sun, 2 Jul 2023 20:50:36 +0200

2023-07-02  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * libpoke/pkl-env.h (pkl_env_new): Update comment to document
        behavior on failure (return `NULL').
        (pkl_env_push_frame): Likewise.
        (pkl_env_dup_toplevel): Likewise.
        * libpoke/pkl-env.c (xalloc.h): Remove include.
        (pkl_env_new): Use `calloc' instead of `xzalloc'.
        (pkl_env_push): Handle memory error.
        (pkl_env_dup_toplevel): Likewise.
        * libpoke/pkl.c (pkl_new): Handle memory failures related to
        `pkl_env'.
        (pkl_execute_buffer): Likewise.
        (pkl_execute_statement): Likewise.
        (pkl_compile_expression): Likewise.
        (pkl_execute_expression): Likewise.
        (pkl_execute_file): Likewise.
---
 ChangeLog         | 18 ++++++++++++++++++
 libpoke/pkl-env.c |  8 +++++---
 libpoke/pkl-env.h |  6 +++---
 libpoke/pkl.c     | 24 +++++++++++++++++++-----
 4 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1756799a..287d7905 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2023-07-02  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * libpoke/pkl-env.h (pkl_env_new): Update comment to document
+       behavior on failure (return `NULL').
+       (pkl_env_push_frame): Likewise.
+       (pkl_env_dup_toplevel): Likewise.
+       * libpoke/pkl-env.c (xalloc.h): Remove include.
+       (pkl_env_new): Use `calloc' instead of `xzalloc'.
+       (pkl_env_push): Handle memory error.
+       (pkl_env_dup_toplevel): Likewise.
+       * libpoke/pkl.c (pkl_new): Handle memory failures related to
+       `pkl_env'.
+       (pkl_execute_buffer): Likewise.
+       (pkl_execute_statement): Likewise.
+       (pkl_compile_expression): Likewise.
+       (pkl_execute_expression): Likewise.
+       (pkl_execute_file): Likewise.
+
 2023-06-28  Jose E. Marchesi  <jemarch@gnu.org>
 
        * poke/pk-ios.c (pk_open_proc_maps): Fix access to uninitialized
diff --git a/libpoke/pkl-env.c b/libpoke/pkl-env.c
index e45a20cf..406d599b 100644
--- a/libpoke/pkl-env.c
+++ b/libpoke/pkl-env.c
@@ -19,7 +19,6 @@
 #include <config.h>
 
 #include <stdlib.h>
-#include <xalloc.h>
 #include <string.h>
 #include <assert.h>
 
@@ -185,7 +184,7 @@ get_ns_table (pkl_env env, int namespace)
 pkl_env
 pkl_env_new ()
 {
-  return xzalloc (sizeof (struct pkl_env));
+  return calloc (1, sizeof (struct pkl_env));
 }
 
 void
@@ -205,7 +204,8 @@ pkl_env_push_frame (pkl_env env)
 {
   pkl_env frame = pkl_env_new ();
 
-  frame->up = env;
+  if (frame)
+    frame->up = env;
   return frame;
 }
 
@@ -384,6 +384,8 @@ pkl_env_dup_toplevel (pkl_env env)
   assert (pkl_env_toplevel_p (env));
 
   new = pkl_env_new ();
+  if (!new)
+    return NULL;
 
   for (i = 0; i < HASH_TABLE_SIZE; ++i)
     {
diff --git a/libpoke/pkl-env.h b/libpoke/pkl-env.h
index 1bc4f9dd..10e2a1cd 100644
--- a/libpoke/pkl-env.h
+++ b/libpoke/pkl-env.h
@@ -57,7 +57,7 @@
 
 typedef struct pkl_env *pkl_env;  /* Struct defined in pkl-env.c */
 
-/* Get an empty environment.  */
+/* Get an empty environment.  Return NULL on failure.  */
 
 pkl_env pkl_env_new (void);
 
@@ -66,7 +66,7 @@ pkl_env pkl_env_new (void);
 void pkl_env_free (pkl_env env);
 
 /* Push a new frame to ENV and return the modified environment.  The
-   new frame is empty.  */
+   new frame is empty.  Return NULL on failure.  */
 
 pkl_env pkl_env_push_frame (pkl_env env);
 
@@ -92,7 +92,7 @@ int pkl_env_register (pkl_env env,
 int pkl_env_toplevel_p (pkl_env env);
 
 /* Return a copy of ENV.  Note this only works for top-level
-   environments.  */
+   environments.  Return NULL on failure.  */
 
 pkl_env pkl_env_dup_toplevel (pkl_env env);
 
diff --git a/libpoke/pkl.c b/libpoke/pkl.c
index 9da052ed..da9c416c 100644
--- a/libpoke/pkl.c
+++ b/libpoke/pkl.c
@@ -106,13 +106,17 @@ pkl_new (pvm vm, const char *rt_path,
 {
   pkl_compiler compiler
     = calloc (1, sizeof (struct pkl_compiler));
+  pkl_env env;
 
   if (!compiler)
     goto out_of_memory;
 
   /* Create the top-level compile-time environment.  This will be used
      for as long as the incremental compiler lives.  */
-  compiler->env = pkl_env_new ();
+   env = pkl_env_new ();
+   if (!env)
+     goto out_of_memory;
+   compiler->env = env;
 
   /* Set the poke virtual machine that the compiler will be generating
      code for.  */
@@ -317,8 +321,10 @@ pkl_execute_buffer (pkl_compiler compiler,
   pkl_env env = NULL;
   pvm_val val;
 
-  compiler->compiling = PKL_COMPILING_PROGRAM;
   env = pkl_env_dup_toplevel (compiler->env);
+  if (!env)
+    goto error;
+  compiler->compiling = PKL_COMPILING_PROGRAM;
 
   /* Parse the input routine into an AST.  */
   ret = pkl_parse_buffer (compiler, &env, &ast,
@@ -371,8 +377,10 @@ pkl_execute_statement (pkl_compiler compiler,
   int ret;
   pkl_env env = NULL;
 
-  compiler->compiling = PKL_COMPILING_STATEMENT;
   env = pkl_env_dup_toplevel (compiler->env);
+  if (!env)
+    goto error;
+  compiler->compiling = PKL_COMPILING_STATEMENT;
 
   /* Parse the input routine into an AST.  */
   ret = pkl_parse_buffer (compiler, &env, &ast,
@@ -421,8 +429,10 @@ pkl_compile_expression (pkl_compiler compiler,
   int ret;
   pkl_env env = NULL;
 
-   compiler->compiling = PKL_COMPILING_EXPRESSION;
    env = pkl_env_dup_toplevel (compiler->env);
+   if (!env)
+     goto error;
+   compiler->compiling = PKL_COMPILING_EXPRESSION;
 
    /* Parse the input program into an AST.  */
    ret = pkl_parse_buffer (compiler, &env, &ast,
@@ -469,8 +479,10 @@ pkl_execute_expression (pkl_compiler compiler,
   int ret;
   pkl_env env = NULL;
 
-  compiler->compiling = PKL_COMPILING_EXPRESSION;
   env = pkl_env_dup_toplevel (compiler->env);
+  if (!env)
+    goto error;
+  compiler->compiling = PKL_COMPILING_EXPRESSION;
 
   /* Parse the input routine into an AST.  */
   ret = pkl_parse_buffer (compiler, &env, &ast,
@@ -531,6 +543,8 @@ pkl_execute_file (pkl_compiler compiler, const char *fname,
     }
 
   env = pkl_env_dup_toplevel (compiler->env);
+  if (!env)
+    goto error;
   ret = pkl_parse_file (compiler, &env,  &ast, fp, fname);
   if (ret == 1)
     /* Parse error.  */
-- 
2.41.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]