Skip to content

Conversation

@stasadev
Copy link
Contributor

@stasadev stasadev commented Jul 14, 2025

There is no check for empty key, which can cause panic:

To reproduce:

services:
  test:
    environment:
      - ""
$ docker-compose -f - config <<< $'services:\n  test:\n    environment:\n      - ""'
panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/compose-spec/compose-go/v2/types.(*MappingWithEquals).DecodeMapstructure(0xc000244e08, {0x5634b5a76ba0?, 0xc000010570?})
        github.com/compose-spec/compose-go/v2@v2.7.1/types/mapping.go:98 +0x34c
github.com/compose-spec/compose-go/v2/loader.decoderHook({0x5634b5a76ba0?, 0xc000010570?, 0x4003f0bfa8?}, {0x5634b5e5f000?, 0xc000244e08?, 0xc00091fcf0?})
        github.com/compose-spec/compose-go/v2@v2.7.1/loader/mapstructure.go:52 +0x30e
...
...

Or:

services:
  test:
    environment:
      - "=value"
$ docker-compose -f - config <<< $'services:\n  test:\n    environment:\n      - "=value"'
panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/compose-spec/compose-go/v2/types.(*MappingWithEquals).DecodeMapstructure(0xc000244e08, {0x55c24bbe3ba0?, 0xc00014e240?})
        github.com/compose-spec/compose-go/v2@v2.7.1/types/mapping.go:98 +0x34c
github.com/compose-spec/compose-go/v2/loader.decoderHook({0x55c24bbe3ba0?, 0xc00014e240?, 0x7ff718638be8?}, {0x55c24bfcc000?, 0xc000244e08?, 0x7ff7633d4108?})
...

@stasadev stasadev requested a review from ndeloof as a code owner July 14, 2025 09:47
@stasadev stasadev force-pushed the fix-panic branch 2 times, most recently from 232c7e4 to 2c725e1 Compare July 14, 2025 09:51
@stasadev stasadev changed the title fix: prevent panic on empty key fix: check for empty key in environment before accessing it by index Jul 14, 2025
@rfay
Copy link

rfay commented Jul 14, 2025

Problem reported in Drupal slack by DDEV end-user.

@ndeloof ndeloof enabled auto-merge (rebase) July 15, 2025 05:47
Signed-off-by: Stanislav Zhuk <stasadev@gmail.com>
@ndeloof ndeloof merged commit 924bff9 into compose-spec:main Jul 15, 2025
8 checks passed
@stasadev stasadev deleted the fix-panic branch July 15, 2025 06:27
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