Skip to content

Fix nil dereference when core uninstall fails during upgrade #1371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

matthijskooijman
Copy link
Collaborator

The code tried to log the wrong error, which was nil, and resulted in a
nil dereference.

This would occur for example when a version of the core was already
installed manually in the sketchbook, and you would try to install
another version using arduino-cli, uninstalling the previous version
would fail (not managed by package manager) and the nil dereference
would happen:

$ arduino-cli core install arduino:avr@1.8.2
Tool arduino:avr-gcc@7.3.0-atmel3.6.1-arduino5 already installed
Tool arduino:avrdude@6.3.0-arduino17 already installed
Tool arduino:arduinoOTA@1.3.0 already installed
Downloading packages...
arduino:avr@1.8.2 already downloaded
Upgrading arduino:avr@1.8.3 with arduino:avr@1.8.2...
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0xde10dc]

goroutine 1 [running]:
github.com/arduino/arduino-cli/commands/core.installPlatform(0xc00038b600, 0xc0002406e0, 0xc0002ea560, 0x3, 0x4, 0xc0002ea540, 0xc0015c11d0, 0x0, 0x58, 0x68)
    /home/matthijs/docs/Electronics/Arduino/arduino-cli/commands/core/install.go:148 +0x10fc
github.com/arduino/arduino-cli/commands/core.PlatformInstall(0x1203c00, 0xc0000360f0, 0xc0000aef50, 0xc0002ea540, 0xc0015c11d0, 0x1, 0x1, 0x0)
    /home/matthijs/docs/Electronics/Arduino/arduino-cli/commands/core/install.go:52 +0x2ce
github.com/arduino/arduino-cli/cli/core.runInstallCommand(0xc000422840, 0xc0003b1a10, 0x1, 0x1)
    /home/matthijs/docs/Electronics/Arduino/arduino-cli/cli/core/install.go:103 +0x2a5
github.com/spf13/cobra.(*Command).execute(0xc000422840, 0xc0003b19f0, 0x1, 0x1, 0xc000422840, 0xc0003b19f0)
    /home/matthijs/docs/src/go/pkg/mod/github.com/spf13/cobra@v1.0.1-0.20200710201246-675ae5f5a98c/command.go:846 +0x2c2
github.com/spf13/cobra.(*Command).ExecuteC(0xc000322dc0, 0x3a, 0xc00037f680, 0xc0001f6a80)
    /home/matthijs/docs/src/go/pkg/mod/github.com/spf13/cobra@v1.0.1-0.20200710201246-675ae5f5a98c/command.go:950 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
    /home/matthijs/docs/src/go/pkg/mod/github.com/spf13/cobra@v1.0.1-0.20200710201246-675ae5f5a98c/command.go:887
main.main()
    /home/matthijs/docs/Electronics/Arduino/arduino-cli/main.go:31 +0x8a

With this fix applied, this produces a proper error message:

$ arduino-cli core install arduino:avr@1.8.2
Tool arduino:avr-gcc@7.3.0-atmel3.6.1-arduino5 already installed
Tool arduino:avrdude@6.3.0-arduino17 already installed
Tool arduino:arduinoOTA@1.3.0 already installed
Downloading packages...
arduino:avr@1.8.2 already downloaded
Upgrading arduino:avr@1.8.3 with arduino:avr@1.8.2...
Error upgrading platform: arduino:avr@1.8.3 is not managed by package manager...
Error during install: upgrading platform: arduino:avr@1.8.3 is not managed by package manager

Please check if the PR fulfills these requirements

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • The PR follows
    our contributing guidelines
  • Tests for the changes have been added (for bug fixes / features) Sorry, that would need quite a bit of work for such a trivial fix, no time for that.
  • Docs have been added / updated (for bug fixes / features) Not applicable
  • UPGRADING.md has been updated with a migration guide (for breaking changes) Not applicable

@cmaglie
Copy link
Member

cmaglie commented Aug 10, 2021

@matthijskooijman can you rebase this one?

@matthijskooijman
Copy link
Collaborator Author

@cmaglie, Done. Turns out some other change already fixed the nil-dereference and reduced it to printing %!s(<nil>, which is still not ideal. I've updated this PR to fix that and properly print the error, and updated the commit message as well.

@cmaglie
Copy link
Member

cmaglie commented Aug 11, 2021

I cannot see it, did you forget to push?
(since you're there if you can rebase again on the current master will also fix a problem in the test runners)

The code tried to log the wrong error, which was nil, hiding the real
error.

This originally occurred for example when a version of the core was already
installed manually in the sketchbook, and you would try to install
another version using arduino-cli, though that has since been fixed
(arduino-cli no longer tries to uninstall cores from the sketchbook).

To still reproduce this problem, you can e.g. break the permissions of
an installed core:

    $ arduino-cli core install arduino:avr@1.8.1
    (...)
    $ chmod a-w ~/.arduino15/packages/arduino/hardware/avr/1.8.1
    $ arduino-cli core install arduino:avr@1.8.2
    Tool arduino:avr-gcc@7.3.0-atmel3.6.1-arduino5 already installed
    Tool arduino:avrdude@6.3.0-arduino17 already installed
    Tool arduino:arduinoOTA@1.3.0 already installed
    Downloading packages...
    arduino:avr@1.8.2 already downloaded
    Upgrading arduino:avr@1.8.1 with arduino:avr@1.8.2...
    Error upgrading platform: %!s(<nil>)...
    Error during install: upgrading platform: removing platform files: unlinkat /home/matthijs/.arduino15/packages/arduino/hardware/avr/1.8.1/firmwares: permission denied

With this fix applied, this produces a proper error message:

    $ arduino-cli core install arduino:avr@1.8.2
    Tool arduino:avr-gcc@7.3.0-atmel3.6.1-arduino5 already installed
    Tool arduino:avrdude@6.3.0-arduino17 already installed
    Tool arduino:arduinoOTA@1.3.0 already installed
    Downloading packages...
    arduino:avr@1.8.2 already downloaded
    Upgrading arduino:avr@1.8.1 with arduino:avr@1.8.2...
    Error upgrading platform: removing platform files: unlinkat /home/matthijs/.arduino15/packages/arduino/hardware/avr/1.8.1/firmwares: permission denied...
    Error during install: upgrading platform: removing platform files: unlinkat /home/matthijs/.arduino15/packages/arduino/hardware/avr/1.8.1/firmwares: permission denied
@matthijskooijman matthijskooijman force-pushed the fix-nil-on-core-uninstall-failure branch from 8d10189 to dda68ac Compare August 11, 2021 12:21
@matthijskooijman
Copy link
Collaborator Author

w00ps, I pushed to this repo, rather than my fork accidentally :-)

I rebased again (I didn't retest, but it seems unlikely that the latest changes would break this), and pushed to the right repo this time (and cleaned up the accidentally pushed branch too).

@matthijskooijman
Copy link
Collaborator Author

Hm, seems there is still some problem with codecov in the tests: https://github.com/matthijskooijman/arduino-cli/runs/3300616293?check_suite_focus=true

@silvanocerza
Copy link
Contributor

Hm, seems there is still some problem with codecov in the tests: https://github.com/matthijskooijman/arduino-cli/runs/3300616293?check_suite_focus=true

That's normal, you're missing the credentials secrets to push from your fork. Don't worry about it, if the PR checks are passing it's fine.

There was a spurious integration tests failure but I restarted the workflows, hope it doesn't fail again.

@cmaglie cmaglie merged commit f632a9d into arduino:master Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants