lilypond-devel
[Top][All Lists]
Advanced

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

Re: Unsecure assoc calls


From: Michael Käppler
Subject: Re: Unsecure assoc calls
Date: Fri, 11 Sep 2009 19:36:16 +0200
User-agent: Thunderbird 2.0.0.12 (X11/20071114)

Hi Carl,
LY_DEFINE (ly_assoc_get, "ly:assoc-get",
          2, 1, 0,

This line is wrong; you need to have the total of these three numbers
be the total number of arguments.
Yes, I forgot to adjust this.
If you change this to 2, 2, 0, then strict-checking will be an optional
argument, and all of the existing calls will work as is.
That's what I'd prefer.
-          (SCM key, SCM alist, SCM default_value),
+          (SCM key, SCM alist, SCM default_value, SCM strict_checking),
          "Return value if @var{key} in @var{alist}, else
@code{default-value}"
          " (or @code{#f} if not specified).")


IIRC, programming errors are not internationalized.  Is that your
understanding?
Yes, why do you ask?
Of course that would also happen with chain-assoc-get.
I also recognized that some assoc-get or chain-assoc-get calls occur
with #f set as default value, which I think is unnecessary since #f is
the hardcoded default already.

It's probably not necessary, but if you really want the default value of #f,
it's probably best to include it in the code.  It conveys the intent more
clearly to the reader, and doesn't cost anything in performance, IMO.
I don't have a clear opinion about this. If you say it makes it more clear to understand, I won't contradict you.

Attached is the revised version. Okay to apply?
The next step would be to change all assoc calls to assoc-get calls. That would be no great deal. More difficult will be to decide to which of the two classes the assoc-get calls belong. Maybe a task for Mark Polesky?

Regards,
Michael


>From 8fa96593e20a00107785b024201d30875afa54cc Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Michael=20K=C3=A4ppler?= <address@hidden>
Date: Fri, 11 Sep 2009 19:16:45 +0200
Subject: [PATCH] Improve error checking in ly:assoc-get and ly:chain-assoc-get.

* Introduce a new optional argument strict_checking

* When strict_checking is set to true, output a programming_error
  if the given key is not found in the given alist / achain.

* This patch does not change the current behaviour. It prepares
  a greater modification to remove all assoc calls through
  secure assoc-get calls.
---
 lily/general-scheme.cc |   28 +++++++++++++++++++---------
 1 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/lily/general-scheme.cc b/lily/general-scheme.cc
index 43ff745..7ef8c0c 100644
--- a/lily/general-scheme.cc
+++ b/lily/general-scheme.cc
@@ -154,10 +154,12 @@ LY_DEFINE (ly_dir_p, "ly:dir?",
 }
 
 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
-          2, 1, 0,
-          (SCM key, SCM alist, SCM default_value),
-          "Return value if @var{key} in @var{alist}, else @code{default-value}"
-          " (or @code{#f} if not specified).")
+          2, 2, 0,
+          (SCM key, SCM alist, SCM default_value, SCM strict_checking),
+          "Return value if @var{key} in @var{alist}, else @var{default_value}"
+          " (or @code{#f} if not specified). If @var{strict_checking} is set"
+           " to @code{#t} and @var{key} is not in @var{alist}, a 
programming_error"
+           " is outputted.")
 {
   LY_ASSERT_TYPE(ly_cheap_is_list, alist, 2);
   
@@ -168,6 +170,9 @@ LY_DEFINE (ly_assoc_get, "ly:assoc-get",
   if (default_value == SCM_UNDEFINED)
     default_value = SCM_BOOL_F;
 
+  if (strict_checking == SCM_BOOL_T)
+    programming_error ("Cannot find key ~S in alist, setting to ~S.", 
ly_scm2string (key), ly_scm2string (default_value));
+
   return default_value;
 }
 
@@ -312,10 +317,11 @@ LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
 }
 
 LY_DEFINE (ly_chain_assoc_get, "ly:chain-assoc-get",
-          2, 1, 0, (SCM key, SCM achain, SCM val),
+          2, 2, 0, (SCM key, SCM achain, SCM default_value, SCM 
strict_checking),
           "Return value for @var{key} from a list of alists @var{achain}."
-          "  If no entry is found, return @var{val} or @code{#f} if"
-          " @var{val} is not specified.")
+          "  If no entry is found, return @var{default_value} or @code{#f} if"
+          " @var{default_value} is not specified. With @var{strict_checking}"
+           " set to @code{#t}, a programming_error is outputted in such 
cases.")
 {
   if (scm_is_pair (achain))
     {
@@ -323,9 +329,13 @@ LY_DEFINE (ly_chain_assoc_get, "ly:chain-assoc-get",
       if (scm_is_pair (handle))
        return scm_cdr (handle);
       else
-       return ly_chain_assoc_get (key, scm_cdr (achain), val);
+       return ly_chain_assoc_get (key, scm_cdr (achain), default_value);
     }
-  return val == SCM_UNDEFINED ? SCM_BOOL_F : val;
+  
+  if (strict_checking == SCM_BOOL_T)
+    programming_error ("Cannot find key ~S in achain, setting to ~S.", 
ly_scm2string (key), ly_scm2string (default_value));
+  
+  return default_value == SCM_UNDEFINED ? SCM_BOOL_F : default_value;
 }
 
 
-- 
1.6.0.2


reply via email to

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