Skip to content

src: use json_object_from_file/fd instead of read_file/fread_file - #175

Merged
giuseppe merged 2 commits into
containers:mainfrom
giuseppe:stream-json-doc
Aug 3, 2026
Merged

giuseppe merged 2 commits into
containers:mainfrom
giuseppe:stream-json-doc

Conversation

@giuseppe

Copy link
Copy Markdown
Member

Replace the custom read-file.c/read-file.h (Apache-2.0 licensed) with json-c's json_object_from_file() and json_object_from_fd(), which parse JSON directly from a file path or descriptor without buffering the entire file into memory first.

This resolves the license incompatibility with GPL-2.0 consumers like crun (issue #174).

Closes: #174

@giuseppe

Copy link
Copy Markdown
Member Author

@jnovy PTAL

Comment thread src/ocispec/sources.py
if (ctx == NULL)
ctx = (const struct parser_context *) (&tmp_ctx);

fd = fileno (stream);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

fileno() returns -1 for FILE* streams created by fmemopen(), open_memstream(), or any other non-fd-backed stream. This breaks libocispec's own fuzzer (src/ocispec/validate.c), which uses fmemopen()

Instead of fileno() + json_object_from_fd(), use fread() in a loop to read the stream contents, then parse with json_tokener_parse()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

thanks, what do you think of the new version?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

nevermind, it breaks the CI, taking a look...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

finally green

@giuseppe
giuseppe force-pushed the stream-json-doc branch 2 times, most recently from d9f86fe to ac0882b Compare July 31, 2026 13:02
The gen_json_map_*() functions pass map keys and values directly to
strlen() without checking for NULL, causing a SIGSEGV when the fuzzer
produces JSON with null values in string maps.

Skip entries with NULL keys, and emit json null for NULL string values.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
@jnovy

jnovy commented Jul 31, 2026

Copy link
Copy Markdown

LGTM

I'd add ferror() check after the fread loop (small robustness improvement) Something like this:

  while ((nread = fread (tmp, 1, sizeof (tmp), stream)) > 0)
    { ... }
  if (buf == NULL)  # only checks empty stream, not I/O error

fread() returns 0 on both EOF and error. Without checking ferror(stream) after the loop, an I/O error mid-stream will be silently treated as EOF, and the code will attempt to parse whatever partial data was read. The old fread_file() code did check ferror(stream). Suggested fix:

  if (ferror (stream))
    {
      *err = strdup ("error reading file stream");
      return NULL;
    }

Replace the custom read-file.c/read-file.h (Apache-2.0 licensed) with
json-c's json_object_from_file() and json_object_from_fd(), which parse
JSON directly from a file path or descriptor without buffering the
entire file into memory first.

This resolves the license incompatibility with GPL-2.0 consumers
like crun (issue containers#174).

Closes: containers#174
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
@giuseppe

Copy link
Copy Markdown
Member Author

good call! Added

@giuseppe

giuseppe commented Aug 3, 2026

Copy link
Copy Markdown
Member Author

@jnovy OK with the last version?

@flouthoc flouthoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@jnovy

jnovy commented Aug 3, 2026

Copy link
Copy Markdown

LGTM

@giuseppe
giuseppe merged commit 872b8b0 into containers:main Aug 3, 2026
7 checks passed
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.

Licensing: read-file.c/read-file.h

3 participants