Skip to content
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

Enable compatibility with Linux (and other) kernel #836

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

greenduck
Copy link

The purpose of this PR is to enable using cJSON in the kernel space, in particular Linux kernel.
This requirement has 3 consequences:

  • no floating point numbers
  • Linux-specific header files
  • refrain from using insecure functions (sprintf())

When building as a part of Linux kernel code, the KERNEL macro will be recognized automatically.
In order to build with no floating point numbers, introduce NO_FLOATING_POINT macro. This macro is automatically introduced in case KERNEL is present.

This PR strives to keep all the existing functionality unchanged.

Replace sprintf() usage with the safer snprintf().
Since snprintf() was introduced only in C99, we have to upgrade the
minimum C standard.

Signed-off-by: Andrey Gelman <[email protected]>
Introduce a rigorous FP value parsing function, that identifies
precisely the number of bytes that can be skipped in the input.
After this, we can afford to drop strtod() in favor of a more robust
sscanf().

Signed-off-by: Andrey Gelman <[email protected]>
Enable building without FP support (cmake -DNO_FLOATING_POINT=TRUE ...)
This is required as a part of the effort to make cJSON available in the
kernel mode.

Notice,
since the tests rely on FP availability, we had to disable tests in
case of NO_FLOATING_POINT.

TODO:
Introduce new tests / adapt existing tests

Signed-off-by: Andrey Gelman <[email protected]>
Adapt cJSON library to serving as a part of a Linux kernel
driver:
- copy cJSON.[ch] to the driver source code tree
- integrate in the build process

Signed-off-by: Andrey Gelman <[email protected]>
@Alanscut
Copy link
Collaborator

As it's declared in README:

cJSON is written in ANSI C (C89) in order to support as many platforms and compilers as possible.

cJSON is designed to use ANSI C/c89. Many users are relying on this. We do not want to break this.

freebsd-git pushed a commit to freebsd/freebsd-ports that referenced this pull request Jun 6, 2024
* Rework Makefile to follow Porters Handbook more closely
* Fix issues reported by DEVELOPER=yes
* Adjust CMAKE args to be readable and there's no need to list
  every possible option
* Compile to c89 standard as other distros and upstream [1]
* Drop static libs, nothing in tree uses them

1: https://github.com/DaveGamble/cJSON/blob/v1.7.18/CMakeLists.txt#L22
   DaveGamble/cJSON#836

PR:		279133
Approved by:	portmgr (maintainer timeout, 2+ weeks)
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.

2 participants