From 1b3264a3aba05e5c07af9ac8f234fb2800b71c51 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Sat, 27 Aug 2022 13:17:15 +0200 Subject: [PATCH] Fixed nil pointer exception in build options extraction --- legacy/builder/libraries_loader.go | 8 ++- .../test/create_build_options_map_test.go | 1 - legacy/builder/types/context.go | 4 +- legacy/builder/types/context_test.go | 61 +++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 legacy/builder/types/context_test.go diff --git a/legacy/builder/libraries_loader.go b/legacy/builder/libraries_loader.go index 8ca114a8a2a..b7e7792adc5 100644 --- a/legacy/builder/libraries_loader.go +++ b/legacy/builder/libraries_loader.go @@ -36,10 +36,12 @@ func (s *LibrariesLoader) Run(ctx *types.Context) error { ctx.LibrariesManager = lm builtInLibrariesFolders := ctx.BuiltInLibrariesDirs - if err := builtInLibrariesFolders.ToAbs(); err != nil { - return errors.WithStack(err) + if builtInLibrariesFolders != nil { + if err := builtInLibrariesFolders.ToAbs(); err != nil { + return errors.WithStack(err) + } + lm.AddLibrariesDir(builtInLibrariesFolders, libraries.IDEBuiltIn) } - lm.AddLibrariesDir(builtInLibrariesFolders, libraries.IDEBuiltIn) if ctx.ActualPlatform != ctx.TargetPlatform { lm.AddPlatformReleaseLibrariesDir(ctx.ActualPlatform, libraries.ReferencedPlatformBuiltIn) diff --git a/legacy/builder/test/create_build_options_map_test.go b/legacy/builder/test/create_build_options_map_test.go index 9cc72379d14..966d86a7205 100644 --- a/legacy/builder/test/create_build_options_map_test.go +++ b/legacy/builder/test/create_build_options_map_test.go @@ -43,7 +43,6 @@ func TestCreateBuildOptionsMap(t *testing.T) { require.Equal(t, `{ "additionalFiles": "", - "builtInLibrariesFolders": "", "builtInToolsFolders": "tools", "compiler.optimization_flags": "-Os", "customBuildProperties": "", diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index a0ad62115de..980f1f410cc 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -208,7 +208,9 @@ func (ctx *Context) ExtractBuildOptions() *properties.Map { opts := properties.NewMap() opts.Set("hardwareFolders", strings.Join(ctx.HardwareDirs.AsStrings(), ",")) opts.Set("builtInToolsFolders", strings.Join(ctx.BuiltInToolsDirs.AsStrings(), ",")) - opts.Set("builtInLibrariesFolders", ctx.BuiltInLibrariesDirs.String()) + if ctx.BuiltInLibrariesDirs != nil { + opts.Set("builtInLibrariesFolders", ctx.BuiltInLibrariesDirs.String()) + } opts.Set("otherLibrariesFolders", strings.Join(ctx.OtherLibrariesDirs.AsStrings(), ",")) opts.SetPath("sketchLocation", ctx.SketchLocation) var additionalFilesRelative []string diff --git a/legacy/builder/types/context_test.go b/legacy/builder/types/context_test.go new file mode 100644 index 00000000000..c10ea71dc33 --- /dev/null +++ b/legacy/builder/types/context_test.go @@ -0,0 +1,61 @@ +// This file is part of arduino-cli. +// +// Copyright 2022 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package types + +import ( + "testing" + + "github.com/arduino/arduino-cli/arduino/cores" + paths "github.com/arduino/go-paths-helper" + "github.com/stretchr/testify/require" +) + +func TestInjectBuildOption(t *testing.T) { + fqbn, err := cores.ParseFQBN("aaa:bbb:ccc") + require.NoError(t, err) + + { + ctx := &Context{ + HardwareDirs: paths.NewPathList("aaa", "bbb"), + BuiltInToolsDirs: paths.NewPathList("ccc", "ddd"), + BuiltInLibrariesDirs: paths.New("eee"), + OtherLibrariesDirs: paths.NewPathList("fff", "ggg"), + SketchLocation: paths.New("hhh"), + FQBN: fqbn, + ArduinoAPIVersion: "iii", + CustomBuildProperties: []string{"jjj", "kkk"}, + OptimizationFlags: "lll", + } + newCtx := &Context{} + newCtx.InjectBuildOptions(ctx.ExtractBuildOptions()) + require.Equal(t, ctx, newCtx) + } + { + ctx := &Context{ + HardwareDirs: paths.NewPathList("aaa", "bbb"), + BuiltInToolsDirs: paths.NewPathList("ccc", "ddd"), + OtherLibrariesDirs: paths.NewPathList("fff", "ggg"), + SketchLocation: paths.New("hhh"), + FQBN: fqbn, + ArduinoAPIVersion: "iii", + CustomBuildProperties: []string{"jjj", "kkk"}, + OptimizationFlags: "lll", + } + newCtx := &Context{} + newCtx.InjectBuildOptions(ctx.ExtractBuildOptions()) + require.Equal(t, ctx, newCtx) + } +}