From d7c4642ab8f28e5943d0c16656d69d35868c7b91 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Fri, 27 Apr 2018 02:49:51 -0400 Subject: [PATCH] Bug 534098 - NPE in VariableHelpers.createType() Change-Id: I0b287d31dba8075d8e99f74d6815d3018c985bc7 --- .../cdt/core/parser/tests/ast2/AST2CPPTests.java | 12 ++++++++++++ .../core/dom/parser/cpp/CPPBuiltinVariable.java | 3 ++- .../internal/core/dom/parser/cpp/CPPVariable.java | 8 ++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java index b66b914fdfd..c20fb763523 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java @@ -12681,4 +12681,16 @@ public class AST2CPPTests extends AST2CPPTestBase { helper.assertVariableType("v_b", CPPBasicType.UNSIGNED_LONG_LONG); helper.assertVariableType("v_c", CPPBasicType.LONG_LONG); } + + // extern void *List[]; + // extern void *List[]; + // void *List[] = { 0 }; + // unsigned int ListSize = sizeof(List)/sizeof(List[0]); + public void testMultipleExternDecls_534098() throws Exception { + BindingAssertionHelper helper = getAssertionHelper(); + IVariable var = helper.assertNonProblem("ListSize"); + // Trigger initial value computation, test that it + // does not throw an exception. + var.getInitialValue(); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBuiltinVariable.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBuiltinVariable.java index 4d601f9d1f5..8f6cb680ab0 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBuiltinVariable.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBuiltinVariable.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.cpp; +import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IScope; @@ -57,7 +58,7 @@ public class CPPBuiltinVariable extends CPPVariable { * Returns null. */ @Override - public IASTNode[] getDeclarations() { + public IASTName[] getDeclarations() { return null; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVariable.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVariable.java index ba153cf6c15..410d9ec0edb 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVariable.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVariable.java @@ -56,7 +56,7 @@ import org.eclipse.core.runtime.PlatformObject; public class CPPVariable extends PlatformObject implements ICPPInternalDeclaredVariable { private IASTName fDefinition; - private IASTName fDeclarations[]; + private IASTName fDeclarations[]; // Allowed to have trailing nulls. Users must check or trim! private IType fType; private IValue fInitialValue = IntegralValue.NOT_INITIALIZED; private boolean fAllResolved; @@ -119,8 +119,8 @@ public class CPPVariable extends PlatformObject implements ICPPInternalDeclaredV } @Override - public IASTNode[] getDeclarations() { - return fDeclarations; + public IASTName[] getDeclarations() { + return fDeclarations == null ? null : ArrayUtil.trim(fDeclarations); } @Override @@ -136,7 +136,7 @@ public class CPPVariable extends PlatformObject implements ICPPInternalDeclaredV boolean allResolved = fAllResolved; fAllResolved = true; - fType = VariableHelpers.createType(this, fDefinition, fDeclarations, allResolved); + fType = VariableHelpers.createType(this, fDefinition, getDeclarations(), allResolved); return fType; }