Skip to content

init: fix unescape_string() not advancing past escape characters#679

Merged
slp merged 1 commit into
containers:mainfrom
dustymabe:dusty-quoting-fix
May 14, 2026
Merged

init: fix unescape_string() not advancing past escape characters#679
slp merged 1 commit into
containers:mainfrom
dustymabe:dusty-quoting-fix

Conversation

@dustymabe
Copy link
Copy Markdown
Contributor

The unescape_string() function in init.c, which handles JSON escape sequences when parsing environment variables from .krun_config.json, had a bug where the pointer 'val' was not advanced past the escape character after processing it.

When encountering a two-character JSON escape sequence like \n or ", the switch statement pre-increments val to point at the escape character (e.g., 'n' or '"') and writes the unescaped byte to the output. However, it never advances val past that character. On the next loop iteration, the character is not a backslash, so it gets copied again as a literal character.

This causes:

  • \n (JSON-escaped newline) to produce a newline followed by a literal 'n'
  • " (JSON-escaped double quote) to produce two double quotes

For example, an environment variable set to a JSON string like:

{"key": "value"}

would be rendered inside the krun VM as:

{""key"": ""value""}

Fix this by adding val++ after writing the unescaped character in each case of the switch statement. The 'u' (unicode) case already handles its own pointer arithmetic and is not affected.

Fixes: #678
Assisted-by: <anthropic/claude-opus-4.6>

The unescape_string() function in init.c, which handles JSON escape
sequences when parsing environment variables from .krun_config.json,
had a bug where the pointer 'val' was not advanced past the escape
character after processing it.

When encountering a two-character JSON escape sequence like \n or \",
the switch statement pre-increments val to point at the escape
character (e.g., 'n' or '"') and writes the unescaped byte to the
output. However, it never advances val past that character. On the
next loop iteration, the character is not a backslash, so it gets
copied again as a literal character.

This causes:
 - \n (JSON-escaped newline) to produce a newline followed by a
   literal 'n'
 - \" (JSON-escaped double quote) to produce two double quotes

For example, an environment variable set to a JSON string like:

  {\"key\": \"value\"}

would be rendered inside the krun VM as:

  {""key"": ""value""}

Fix this by adding val++ after writing the unescaped character in
each case of the switch statement. The 'u' (unicode) case already
handles its own pointer arithmetic and is not affected.

Fixes: containers#678
Assisted-by: <anthropic/claude-opus-4.6>
Signed-off-by: Dusty Mabe <dusty@dustymabe.com>
@dustymabe
Copy link
Copy Markdown
Contributor Author

Please add quite a bit of scrutiny here. I definitely don't know this code base.

In my local test this fixes at least the problem from #678:

[dustymabe@hattop ~]$ export JSON_VAR='
{
    "var": "value",
    "var2": "value2",
}'
[dustymabe@hattop ~]$ podman run -it --rm -e JSON_VAR quay.io/fedora/fedora:44 echo "$JSON_VAR"
Trying to pull quay.io/fedora/fedora:44...
Getting image source signatures
Copying blob dfa92e469035 done   | 
Copying config a01ae4eb9c done   | 
Writing manifest to image destination

{
    "var": "value",
    "var2": "value2",
}
[dustymabe@hattop ~]$ podman run -it --rm --runtime=krun -e JSON_VAR quay.io/fedora/fedora:44 echo "$JSON_VAR"

{
    "var": "value",
    "var2": "value2",
}

Copy link
Copy Markdown
Collaborator

@slp slp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @dustymabe !

@slp slp merged commit 60fe4f6 into containers:main May 14, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

quoting issues for environment variables passed from podman

2 participants