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

sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git

commit 96bf3cdd1cbe263923e249bd5e9aef1741ce8afa
Author: Szymon Janc <[email protected]>
AuthorDate: Mon Oct 19 17:48:25 2026 +0200

    newt/syscfg: Fix crash on override of newt-injected settings
    
    Settings injected by newt itself (TARGET_NAME, BSP_NAME, ARCH_NAME,
    APP_NAME, NEWT_FEATURE_*, ...) have no defining package. Overriding
    one of them from a package, e.g. TARGET_NAME in a target's syscfg.yml,
    is reported as a priority violation, but generating the error text
    dereferenced the nil defining package and newt crashed. The same
    happened when dumping the configuration.
    
    Report such an override as a setting that is set by newt and cannot be
    overridden, and use "newt" as the package name in the dump.
    
    Assisted-by: Claude:claude-opus-5-5
---
 newt/dump/syscfg.go                                |  4 +-
 newt/syscfg/syscfg.go                              | 40 +++++++++++++----
 newt/syscfg_stability_test.go                      | 52 ++++++++++++++++++++++
 newt/testdata/syscfg_stability/apps/app/pkg.yml    | 24 ++++++++++
 newt/testdata/syscfg_stability/project.yml         | 25 +++++++++++
 .../repos/apache-mynewt-core/compiler/sim/pkg.yml  | 21 +++++++++
 .../repos/apache-mynewt-core/hw/bsp/sim/bsp.yml    | 28 ++++++++++++
 .../repos/apache-mynewt-core/hw/bsp/sim/pkg.yml    | 21 +++++++++
 .../repos/apache-mynewt-core/repository.yml        | 22 +++++++++
 .../repos/apache-mynewt-core/sys/foo/pkg.yml       | 24 ++++++++++
 .../syscfg_stability/targets/newt_override/pkg.yml | 22 +++++++++
 .../targets/newt_override/syscfg.yml               | 22 +++++++++
 .../targets/newt_override/target.yml               | 23 ++++++++++
 13 files changed, 318 insertions(+), 10 deletions(-)

diff --git a/newt/dump/syscfg.go b/newt/dump/syscfg.go
index 8b9205bf..172bf33c 100644
--- a/newt/dump/syscfg.go
+++ b/newt/dump/syscfg.go
@@ -124,8 +124,8 @@ func convStringMapRestrictionSlice(
 func convPriority(p syscfg.CfgPriority) SyscfgPriority {
        return SyscfgPriority{
                Name:      p.SettingName,
-               Definer:   p.PackageDef.FullName(),
-               Overrider: p.PackageSrc.FullName(),
+               Definer:   p.DefName(),
+               Overrider: p.SrcName(),
        }
 }
 
diff --git a/newt/syscfg/syscfg.go b/newt/syscfg/syscfg.go
index 3e1ed25e..a9d04fcf 100644
--- a/newt/syscfg/syscfg.go
+++ b/newt/syscfg/syscfg.go
@@ -113,6 +113,18 @@ type CfgPriority struct {
        PackageSrc  *pkg.LocalPackage // package overriding setting value.
 }
 
+// Returns the name of the package that defines the setting, or "newt" if
+// the setting is injected by newt.
+func (p CfgPriority) DefName() string {
+       return lpkgNameOrNewt(p.PackageDef)
+}
+
+// Returns the name of the package that overrides the setting, or "newt" if
+// the value is injected by newt.
+func (p CfgPriority) SrcName() string {
+       return lpkgNameOrNewt(p.PackageSrc)
+}
+
 type CfgFlashConflict struct {
        SettingNames []string
        Code         CfgFlashConflictCode
@@ -308,12 +320,17 @@ func (cfg *Cfg) AllSettingsForLpkg(lpkg 
*pkg.LocalPackage) *cfgv.Settings {
        return cfg.settingsForLpkg(lpkg, cfg.SettingValues())
 }
 
-func (point CfgPoint) Name() string {
-       if point.Source == nil {
+// Returns the full name of the package, or "newt" if the package is nil
+// (i.e., the setting or value is injected by newt).
+func lpkgNameOrNewt(lpkg *pkg.LocalPackage) string {
+       if lpkg == nil {
                return "newt"
-       } else {
-               return point.Source.FullName()
        }
+       return lpkg.FullName()
+}
+
+func (point CfgPoint) Name() string {
+       return lpkgNameOrNewt(point.Source)
 }
 
 func (point CfgPoint) IsInjected() bool {
@@ -1059,10 +1076,17 @@ func (cfg *Cfg) ErrorText() string {
                        entry := cfg.Settings[priority.SettingName]
                        historyMap[priority.SettingName] = entry.History
 
-                       str += fmt.Sprintf(
-                               "    Package: %s overriding setting: %s defined 
by %s\n",
-                               priority.PackageSrc.FullName(), 
priority.SettingName,
-                               priority.PackageDef.FullName())
+                       if priority.PackageDef == nil {
+                               str += fmt.Sprintf(
+                                       "    Package: %s overriding setting: %s 
which is set "+
+                                               "by newt and cannot be 
overridden\n",
+                                       priority.SrcName(), 
priority.SettingName)
+                       } else {
+                               str += fmt.Sprintf(
+                                       "    Package: %s overriding setting: %s 
defined by %s\n",
+                                       priority.SrcName(), 
priority.SettingName,
+                                       priority.DefName())
+                       }
                }
        }
 
diff --git a/newt/syscfg_stability_test.go b/newt/syscfg_stability_test.go
new file mode 100644
index 00000000..8a7addee
--- /dev/null
+++ b/newt/syscfg_stability_test.go
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package main
+
+import (
+       "path/filepath"
+       "strings"
+       "testing"
+)
+
+var syscfgStabilityDir = filepath.Join("testdata", "syscfg_stability")
+
+// syscfgStabilityCheckNoPanic fails the test if newt output indicates a
+// crash.
+func syscfgStabilityCheckNoPanic(t *testing.T, out string) {
+       t.Helper()
+
+       if strings.Contains(out, "panic:") ||
+               strings.Contains(out, "goroutine ") {
+               t.Fatalf("newt crashed\n%s", out)
+       }
+}
+
+// Overriding a setting injected by newt (e.g., TARGET_NAME) is an error.
+func TestSyscfgStabilityNewtSettingOverride(t *testing.T) {
+       out, _ := newtCombinedOutput(syscfgStabilityDir, nil, "target",
+               "config", "show", "newt_override")
+       syscfgStabilityCheckNoPanic(t, out)
+
+       msg := "Package: targets/newt_override overriding setting: " +
+               "TARGET_NAME which is set by newt and cannot be overridden"
+       if !strings.Contains(out, msg) {
+               t.Errorf("missing priority violation error\n%s", out)
+       }
+}
diff --git a/newt/testdata/syscfg_stability/apps/app/pkg.yml 
b/newt/testdata/syscfg_stability/apps/app/pkg.yml
new file mode 100644
index 00000000..55bf224b
--- /dev/null
+++ b/newt/testdata/syscfg_stability/apps/app/pkg.yml
@@ -0,0 +1,24 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: apps/app
+pkg.type: app
+
+pkg.deps:
+    - "@apache-mynewt-core/sys/foo"
diff --git a/newt/testdata/syscfg_stability/project.yml 
b/newt/testdata/syscfg_stability/project.yml
new file mode 100644
index 00000000..e67f5141
--- /dev/null
+++ b/newt/testdata/syscfg_stability/project.yml
@@ -0,0 +1,25 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+project.name: "syscfg_stability"
+
+repository.apache-mynewt-core:
+    type: git
+    vers: 0.0.0
+    url: 'https://dummy.invalid/apache-mynewt-core.git'
diff --git 
a/newt/testdata/syscfg_stability/repos/apache-mynewt-core/compiler/sim/pkg.yml 
b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/compiler/sim/pkg.yml
new file mode 100644
index 00000000..2f624582
--- /dev/null
+++ 
b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/compiler/sim/pkg.yml
@@ -0,0 +1,21 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: compiler/sim
+pkg.type: compiler
diff --git 
a/newt/testdata/syscfg_stability/repos/apache-mynewt-core/hw/bsp/sim/bsp.yml 
b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/hw/bsp/sim/bsp.yml
new file mode 100644
index 00000000..934c920b
--- /dev/null
+++ b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/hw/bsp/sim/bsp.yml
@@ -0,0 +1,28 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+bsp.arch: sim
+bsp.compiler: "@apache-mynewt-core/compiler/sim"
+
+bsp.flash_map:
+    areas:
+        FLASH_AREA_IMAGE_0:
+            device: 0
+            offset: 0x00000000
+            size: 64kB
diff --git 
a/newt/testdata/syscfg_stability/repos/apache-mynewt-core/hw/bsp/sim/pkg.yml 
b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/hw/bsp/sim/pkg.yml
new file mode 100644
index 00000000..7eee233c
--- /dev/null
+++ b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/hw/bsp/sim/pkg.yml
@@ -0,0 +1,21 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: hw/bsp/sim
+pkg.type: bsp
diff --git 
a/newt/testdata/syscfg_stability/repos/apache-mynewt-core/repository.yml 
b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/repository.yml
new file mode 100644
index 00000000..aabf9b4b
--- /dev/null
+++ b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/repository.yml
@@ -0,0 +1,22 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+repo.name: apache-mynewt-core
+repo.versions:
+    "0.0.0": "master"
diff --git 
a/newt/testdata/syscfg_stability/repos/apache-mynewt-core/sys/foo/pkg.yml 
b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/sys/foo/pkg.yml
new file mode 100644
index 00000000..5c17e91b
--- /dev/null
+++ b/newt/testdata/syscfg_stability/repos/apache-mynewt-core/sys/foo/pkg.yml
@@ -0,0 +1,24 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: sys/foo
+pkg.type: lib
+
+pkg.init:
+    foo_init: 100
diff --git a/newt/testdata/syscfg_stability/targets/newt_override/pkg.yml 
b/newt/testdata/syscfg_stability/targets/newt_override/pkg.yml
new file mode 100644
index 00000000..60b49d3a
--- /dev/null
+++ b/newt/testdata/syscfg_stability/targets/newt_override/pkg.yml
@@ -0,0 +1,22 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+
+pkg.name: targets/newt_override
+pkg.type: target
diff --git a/newt/testdata/syscfg_stability/targets/newt_override/syscfg.yml 
b/newt/testdata/syscfg_stability/targets/newt_override/syscfg.yml
new file mode 100644
index 00000000..8b503234
--- /dev/null
+++ b/newt/testdata/syscfg_stability/targets/newt_override/syscfg.yml
@@ -0,0 +1,22 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+
+syscfg.vals:
+    TARGET_NAME: '"x"'
diff --git a/newt/testdata/syscfg_stability/targets/newt_override/target.yml 
b/newt/testdata/syscfg_stability/targets/newt_override/target.yml
new file mode 100644
index 00000000..7fbe84da
--- /dev/null
+++ b/newt/testdata/syscfg_stability/targets/newt_override/target.yml
@@ -0,0 +1,23 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+
+target.app: apps/app
+target.bsp: "@apache-mynewt-core/hw/bsp/sim"
+target.build_profile: default

Reply via email to