BUG-1434: fix NPEs when using null byte arrays
authorRobert Varga <rovarga@cisco.com>
Mon, 26 Jan 2015 16:42:35 +0000 (17:42 +0100)
committerRobert Varga <rovarga@cisco.com>
Mon, 26 Jan 2015 16:53:39 +0000 (17:53 +0100)
Commit introduced a regression, which caused NPEs being thrown when the
byte array was null (and hence failed to copy). Guard the copy with an
explicit null check and do not use Arrays.copyOf(), but clone().

Change-Id: I832ccae7a269548954705b2c1cdb1a42f83f71a4
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/UnionTemplate.xtend

index 3cb9d8ce61703976b6d54ab578305aa656b6e980..9e33442de24059144a365aa7e2f4b8c24594976e 100644 (file)
@@ -102,7 +102,7 @@ abstract class BaseTemplate {
         '''
             public «field.returnType.importedName» «field.getterMethodName»() {
                 «IF field.returnType.importedName.contains("[]")»
-                return Arrays.copyOf(«field.fieldName», «field.fieldName».length);
+                return «field.fieldName» == null ? null : «field.fieldName».clone();
                 «ELSE»
                 return «field.fieldName»;
                 «ENDIF»
index 1f9d9d0a4b81a7fe2d497673a08317a26ed9988c..b79d871f886bbacb705050b6fc53ac88a565c359 100644 (file)
@@ -297,7 +297,7 @@ class ClassTemplate extends BaseTemplate {
 
         «FOR p : properties»
             «IF p.returnType.importedName.contains("[]")»
-            this.«p.fieldName» = Arrays.copyOf(«p.fieldName», «p.fieldName».length);
+            this.«p.fieldName» = «p.fieldName» == null ? null : «p.fieldName».clone();
             «ELSE»
             this.«p.fieldName» = «p.fieldName»;
             «ENDIF»
index ecaaf8af01510af20257ba4a72e22ef20d8c7767..1d8a156e2492f5ef7aacc0ed8a3faab0ac523704 100644 (file)
@@ -53,7 +53,7 @@ class UnionTemplate extends ClassTemplate {
                     «FOR other : finalProperties»
                         «IF other.name.equals("value")»
                             «IF other.returnType.importedName.contains("[]")»
-                            this.«other.fieldName» = Arrays.copyOf(«other.fieldName», «other.fieldName».length);
+                            this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
                             «ELSE»
                             this.«other.fieldName» = «other.fieldName»;
                             «ENDIF»
@@ -126,7 +126,7 @@ class UnionTemplate extends ClassTemplate {
             «IF !properties.empty»
                 «FOR p : properties»
                     «IF p.returnType.importedName.contains("[]")»
-                    this.«p.fieldName» = Arrays.copyOf(source.«p.fieldName», source.«p.fieldName».length);
+                    this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
                     «ELSE»
                     this.«p.fieldName» = source.«p.fieldName»;
                     «ENDIF»