You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2020/11/28 14:00:57 UTC

[freemarker] branch 2.3-gae updated: FREEMARKER-165: Fixed bug where where if the namespace expression in a block assignment (like <#assign x in someNamespace>...) refers to a missing variable, or has the wrong type, FreeMarker has thrown NullPounterException or ClassCastException, instead of InvalidReferenceException and NonNamespaceException with proper helpful message.

This is an automated email from the ASF dual-hosted git repository.

ddekany pushed a commit to branch 2.3-gae
in repository https://gitbox.apache.org/repos/asf/freemarker.git


The following commit(s) were added to refs/heads/2.3-gae by this push:
     new d7b9ba1  FREEMARKER-165: Fixed bug where where if the namespace expression in a block assignment (like <#assign x in someNamespace>...</#assign>) refers to a missing variable, or has the wrong type, FreeMarker has thrown NullPounterException or ClassCastException, instead of InvalidReferenceException and NonNamespaceException with proper helpful message.
d7b9ba1 is described below

commit d7b9ba12aee20dd8612edbedec7555241a3a21de
Author: ddekany <dd...@apache.org>
AuthorDate: Sat Nov 28 15:00:15 2020 +0100

    FREEMARKER-165: Fixed bug where where if the namespace expression in a block assignment (like <#assign x in someNamespace>...</#assign>) refers to a missing variable, or has the wrong type, FreeMarker has thrown NullPounterException or ClassCastException, instead of InvalidReferenceException and NonNamespaceException with proper helpful message.
---
 src/main/java/freemarker/core/Assignment.java            |  6 +++---
 src/main/java/freemarker/core/BlockAssignment.java       | 12 +++++++++++-
 src/manual/en_US/book.xml                                | 15 +++++++++++++++
 src/test/java/freemarker/core/MiscErrorMessagesTest.java | 14 +++++++++++++-
 4 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/src/main/java/freemarker/core/Assignment.java b/src/main/java/freemarker/core/Assignment.java
index 0495f14..4d74447 100644
--- a/src/main/java/freemarker/core/Assignment.java
+++ b/src/main/java/freemarker/core/Assignment.java
@@ -118,11 +118,11 @@ final class Assignment extends TemplateElement {
                 throw new BugException("Unexpected scope type: " + scope);
             }
         } else {
-            TemplateModel namespaceTM = namespaceExp.eval(env);
+            TemplateModel uncheckedNamespace = namespaceExp.eval(env);
             try {
-                namespace = (Environment.Namespace) namespaceTM;
+                namespace = (Environment.Namespace) uncheckedNamespace;
             } catch (ClassCastException e) {
-                throw new NonNamespaceException(namespaceExp, namespaceTM, env);
+                throw new NonNamespaceException(namespaceExp, uncheckedNamespace, env);
             }
             if (namespace == null) {
                 throw InvalidReferenceException.getInstance(namespaceExp, env);
diff --git a/src/main/java/freemarker/core/BlockAssignment.java b/src/main/java/freemarker/core/BlockAssignment.java
index 8c9e403..cf54fd6 100644
--- a/src/main/java/freemarker/core/BlockAssignment.java
+++ b/src/main/java/freemarker/core/BlockAssignment.java
@@ -59,7 +59,17 @@ final class BlockAssignment extends TemplateElement {
         }
         
         if (namespaceExp != null) {
-            ((Environment.Namespace) namespaceExp.eval(env)).put(varName, value);
+            final Environment.Namespace namespace;
+            TemplateModel uncheckedNamespace = namespaceExp.eval(env);
+            try {
+                namespace = (Environment.Namespace) uncheckedNamespace;
+            } catch (ClassCastException e) {
+                throw new NonNamespaceException(namespaceExp, uncheckedNamespace, env);
+            }
+            if (namespace == null) {
+                throw InvalidReferenceException.getInstance(namespaceExp, env);
+            }
+            namespace.put(varName, value);
         } else if (scope == Assignment.NAMESPACE) {
             env.setVariable(varName, value);
         } else if (scope == Assignment.GLOBAL) {
diff --git a/src/manual/en_US/book.xml b/src/manual/en_US/book.xml
index 3ba63f6..e87f742 100644
--- a/src/manual/en_US/book.xml
+++ b/src/manual/en_US/book.xml
@@ -29476,6 +29476,21 @@ TemplateModel x = env.getVariable("x");  // get variable x</programlisting>
               the deduced Java 9 module name earlier, but that was fragile, as
               Java has deduced it from the jar file name.</para>
             </listitem>
+
+            <listitem>
+              <para><link
+              xlink:href="https://issues.apache.org/jira/browse/FREEMARKER-165">FREEMARKER-165</link>:
+              Fixed bug where where if the namespace expression in a block
+              assignment (like <literal>&lt;#assign
+              <replaceable>x</replaceable> in
+              <replaceable>someNamespace</replaceable>&gt;<replaceable>...</replaceable>&lt;/#assign&gt;</literal>)
+              refers to a missing variable, or has the wrong type, FreeMarker
+              has thrown <literal>NullPounterException</literal> or
+              <literal>ClassCastException</literal>, instead of
+              <literal>InvalidReferenceException</literal> and
+              <literal>NonNamespaceException</literal> with proper helpful
+              message.</para>
+            </listitem>
           </itemizedlist>
         </section>
       </section>
diff --git a/src/test/java/freemarker/core/MiscErrorMessagesTest.java b/src/test/java/freemarker/core/MiscErrorMessagesTest.java
index df10fde..6e98fcc 100644
--- a/src/test/java/freemarker/core/MiscErrorMessagesTest.java
+++ b/src/test/java/freemarker/core/MiscErrorMessagesTest.java
@@ -62,5 +62,17 @@ public class MiscErrorMessagesTest extends TemplateTest {
         assertErrorContains("<#global x += 2>", "\"x\"", "+=", "global scope");
         assertErrorContains("<#macro m><#local x--></#macro><@m/>", "\"x\"", "--", "local scope");
     }
-    
+
+    @Test
+    public void assignmentNamespaceChecks() {
+        assertErrorContains("<#assign x = 1 in noSuchVar>", InvalidReferenceException.class, "noSuchVar");
+        assertErrorContains("<#assign x =1 in 'notANamespace'>", NonNamespaceException.class, "notANamespace");
+    }
+
+    @Test
+    public void blockAssignmentNamespaceChecks() {
+        assertErrorContains("<#assign x in noSuchVar>1</#assign>", InvalidReferenceException.class, "noSuchVar");
+        assertErrorContains("<#assign x in 'notANamespace'>1</#assign>", NonNamespaceException.class, "notANamespace");
+    }
+
 }