From 65b2ea61fbe33bb5caddc37a32f557ce8de0c803 Mon Sep 17 00:00:00 2001 From: Silvano Cerza <3314350+silvanocerza@users.noreply.github.com> Date: Mon, 12 Jul 2021 10:39:45 +0200 Subject: [PATCH] Fix crash when loading boards (#1354) --- arduino/cores/packagemanager/loader.go | 9 +-- test/test_core.py | 36 ++++++++++++ .../boards.txt | 58 +++++++++++++++++++ .../platform.txt | 2 + 4 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 test/testdata/platform_with_missing_custom_board_options/boards.txt create mode 100644 test/testdata/platform_with_missing_custom_board_options/platform.txt diff --git a/arduino/cores/packagemanager/loader.go b/arduino/cores/packagemanager/loader.go index 58af475c151..0da0171ce96 100644 --- a/arduino/cores/packagemanager/loader.go +++ b/arduino/cores/packagemanager/loader.go @@ -384,6 +384,9 @@ func (pm *PackageManager) loadBoards(platform *cores.PlatformRelease) error { } else { platform.Menus = properties.NewMap() } + // This is not a board id so we remove it to correctly + // set all other boards properties + delete(propertiesByBoard, "menu") if !platform.PluggableDiscoveryAware { for _, boardProperties := range propertiesByBoard { @@ -391,12 +394,6 @@ func (pm *PackageManager) loadBoards(platform *cores.PlatformRelease) error { } } - platform.Menus = propertiesByBoard["menu"] - - // This is not a board id so we remove it to correctly - // set all other boards properties - delete(propertiesByBoard, "menu") - skippedBoards := []string{} for boardID, boardProperties := range propertiesByBoard { var board *cores.Board diff --git a/test/test_core.py b/test/test_core.py index e01517589ec..f396e5736d1 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -712,3 +712,39 @@ def test_core_with_wrong_custom_board_options_is_loaded(run_command, data_dir): + "skipping loading of boards arduino-beta-dev:platform_with_wrong_custom_board_options:nessuno: " + "malformed custom board options" ) in res.stderr + + +def test_core_with_missing_custom_board_options_is_loaded(run_command, data_dir): + test_platform_name = "platform_with_missing_custom_board_options" + platform_install_dir = Path(data_dir, "hardware", "arduino-beta-dev", test_platform_name) + platform_install_dir.mkdir(parents=True) + + # Install platform in Sketchbook hardware dir + shutil.copytree( + Path(__file__).parent / "testdata" / test_platform_name, platform_install_dir, dirs_exist_ok=True, + ) + + assert run_command("update") + + res = run_command("core list --format json") + assert res.ok + + cores = json.loads(res.stdout) + mapped = {core["id"]: core for core in cores} + assert len(mapped) == 1 + # Verifies platform is loaded except excluding board with missing options + assert "arduino-beta-dev:platform_with_missing_custom_board_options" in mapped + boards = {b["fqbn"]: b for b in mapped["arduino-beta-dev:platform_with_missing_custom_board_options"]["boards"]} + assert len(boards) == 1 + # Verify board with malformed options is not loaded + assert "arduino-beta-dev:platform_with_missing_custom_board_options:nessuno" not in boards + # Verify other board is loaded + assert "arduino-beta-dev:platform_with_missing_custom_board_options:altra" in boards + # Verify warning is shown to user + assert ( + "Error initializing instance: " + + "loading platform release arduino-beta-dev:platform_with_missing_custom_board_options@4.2.0: " + + "loading boards: " + + "skipping loading of boards arduino-beta-dev:platform_with_missing_custom_board_options:nessuno: " + + "malformed custom board options" + ) in res.stderr diff --git a/test/testdata/platform_with_missing_custom_board_options/boards.txt b/test/testdata/platform_with_missing_custom_board_options/boards.txt new file mode 100644 index 00000000000..87bbb3b86d8 --- /dev/null +++ b/test/testdata/platform_with_missing_custom_board_options/boards.txt @@ -0,0 +1,58 @@ +nessuno.name=Arduino Nessuno +nessuno.vid.0=0x2341 +nessuno.pid.0=0x0043 +nessuno.vid.1=0x2341 +nessuno.pid.1=0x0001 +nessuno.vid.2=0x2A03 +nessuno.pid.2=0x0043 +nessuno.vid.3=0x2341 +nessuno.pid.3=0x0243 +nessuno.upload.tool=avrdude +nessuno.upload.protocol=arduino +nessuno.upload.maximum_size=32256 +nessuno.upload.maximum_data_size=2048 +nessuno.upload.speed=115200 +nessuno.bootloader.tool=avrdude +nessuno.bootloader.low_fuses=0xFF +nessuno.bootloader.high_fuses=0xDE +nessuno.bootloader.extended_fuses=0xFD +nessuno.bootloader.unlock_bits=0x3F +nessuno.bootloader.lock_bits=0x0F +nessuno.bootloader.file=optiboot/optiboot_atmega328.hex +nessuno.build.mcu=atmega328p +nessuno.build.f_cpu=16000000L +nessuno.build.board=AVR_NESSUNO +nessuno.build.core=arduino +nessuno.build.variant=standard + +nessuno.menu.cache.on=Enabled +nessuno.menu.cache.on.build.cache_flags=-DENABLE_CACHE +nessuno.menu.cache.off=Disabled +nessuno.menu.cache.off.build.cache_flags= + +altra.name=Arduino Altra +altra.vid.0=0x2341 +altra.pid.0=0x0043 +altra.vid.1=0x2341 +altra.pid.1=0x0001 +altra.vid.2=0x2A03 +altra.pid.2=0x0043 +altra.vid.3=0x2341 +altra.pid.3=0x0243 +altra.upload.tool=avrdude +altra.upload.protocol=arduino +altra.upload.maximum_size=32256 +altra.upload.maximum_data_size=2048 +altra.upload.speed=115200 +altra.bootloader.tool=avrdude +altra.bootloader.low_fuses=0xFF +altra.bootloader.high_fuses=0xDE +altra.bootloader.extended_fuses=0xFD +altra.bootloader.unlock_bits=0x3F +altra.bootloader.lock_bits=0x0F +altra.bootloader.file=optiboot/optiboot_atmega328.hex +altra.build.mcu=atmega328p +altra.build.f_cpu=16000000L +altra.build.board=AVR_ALTRA +altra.build.core=arduino +altra.build.variant=standard diff --git a/test/testdata/platform_with_missing_custom_board_options/platform.txt b/test/testdata/platform_with_missing_custom_board_options/platform.txt new file mode 100644 index 00000000000..682f130ed49 --- /dev/null +++ b/test/testdata/platform_with_missing_custom_board_options/platform.txt @@ -0,0 +1,2 @@ +name=Arduino Test Boards +version=4.2.0 \ No newline at end of file