439 Commits
Author SHA1 Message Date
Thiago Macieira ff7a23a8f1 CI: Disable building the tools for macOS small
CI / macos-15-intel/clang (push) Canceled after 0s
CI / ubuntu-latest/clang (push) Canceled after 0s
CI / ubuntu-latest/linux-g++ (push) Canceled after 0s
CI / macos-latest/clang-small (push) Canceled after 0s
CI / ubuntu-latest/clang-small (push) Canceled after 0s
CI / ubuntu-latest/gcc-small (push) Canceled after 0s
CI / ubuntu-latest/gcc-no-math (push) Canceled after 0s
CI / ubuntu-latest/gcc-freestanding (push) Canceled after 0s
The build fails after the transition to Apple silicon and I don't know
why:
```
: && /usr/bin/clang -Oz -g -Werror -arch arm64 -Wl,-search_paths_first -Wl,-headerpad_max_install_names tools/json2cbor/CMakeFiles/json2cbor.dir/json2cbor.c.o -o tools/json2cbor/json2cbor  libtinycbor.a  -lcjson  -lm && :
ld: library 'cjson' not found
```

Homebrew says it installed and CMake says it found it.
```
-- Checking for module 'libcjson'
--   Found libcjson, version 1.7.19
```

I won't investigate.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2026-02-05 15:01:24 -08:00
Thiago Macieira 91d1c50d60 compilersupport: fix compilation in C23 mode
GCC15 added support for C23's `unreachable()`, causing a warning:

```
src/compilersupport_p.h:215:11: warning: ‘unreachable’ redefined
  215 | #  define unreachable() __builtin_unreachable()
      |           ^~~~~~~~~~~
stddef.h:468:9: note: this is the previous definition
  468 | #define unreachable() (__builtin_unreachable ())
      |         ^~~~~~~~~~~
```

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2026-02-05 14:16:59 -08:00
Thiago Macieira 0cd8b054a1 cbortojson: don't hardcode OS support for fopencookie()
Instead of saying Linux (a.k.a. glibc) has it and Apple has funopen(),
use the fact that we've just detected them and inform the .c source
which one it was.

Fixes #306

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2026-02-05 14:16:39 -08:00
Thiago Macieira 7a3b6ab7bc CMake: fix the option() order
I'd forgot to save the file before Git commit...

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2026-02-05 14:16:24 -08:00
Thiago Macieira 64da0f471a CBOR-to-JSON: print integers with full precision
Instead of performing a lossy conversion to double and printing that.
It's irrelevant whether the parser on the other side can store this
precision, only that it can parse this. That includes numbers outside
the range of int64_t, which CBOR does support.

We do this by simply removing code from cbortojson.c and instead just
relying on what cborpretty.c already has.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2026-02-05 14:15:50 -08:00
Thiago Macieira ef6f92349f simplereader: Fix warning about use of pointer variable after free()
We weren't dereferencing the variable, so this should have been safe.
However, it isn't clear in the C and C++ standards whether it
legitimately was safe. So let's just fix it.

```
simplereader.c:180:9: warning: pointer ‘buf’ may be used after ‘free’ [-Wuse-after-free]
simplereader.c:177:5: note: call to ‘free’ here
```

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2026-02-05 14:09:42 -08:00
Thiago Macieira 9487c1b3b4 CI: update the macOS images to more modern versions
macos-13 no longer exists in GitHub Actions.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2026-02-05 14:07:12 -08:00
Thiago Macieira 09496c6432 compilersupport: add support for C23's nullptr
It has the same semantics as C++11's `nullptr`.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-12-01 13:25:03 -08:00
dependabot[bot] 17362494e2 Bump actions/checkout from 5 to 6
Bumps [actions/checkout](https://github.com/actions/checkout) from 5 to 6.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-11-24 09:47:16 -08:00
Florian Larysch 48a22bddfc fix build on i386 without SSE2
Commit 3cba6b1 ("Use _Float16 for half conversions if available") added
support for using half-width float support in the compiler to perform
encoding operations, using the FLT16_MANT_DIG macro to check for
support on the given target.

However, on x86 GCC only supports this when SSE2 is enabled[1]. Unlike
clang and the other architectures where support for this is conditional,
GCC *does* define those macros even without SSE2 support, causing a
build failure:

  In file included from cborencoder_float.c:29:
  cborinternal_p.h: In function ‘encode_half’:
  cborinternal_p.h:56:5: error: invalid conversion to type ‘_Float16’ without option ‘-msse2’
     56 |     _Float16 f = (_Float16)x;
        |     ^~~~~~~~
  cborinternal_p.h: In function ‘decode_half’:
  cborinternal_p.h:65:5: error: invalid conversion from type ‘_Float16’ without option ‘-msse2’
     65 |     return (float)f;
        |

Work around this by additionally checking for this specific condition.

[1] https://gcc.gnu.org/onlinedocs/gcc/Half-Precision.html

Signed-off-by: Florian Larysch <fl@n621.de>
2025-08-19 13:46:12 -07:00
dependabot[bot] 3982a3e853 Bump actions/checkout from 4 to 5
Bumps [actions/checkout](https://github.com/actions/checkout) from 4 to 5.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-08-12 10:02:40 -07:00
jhlee525 8c72b3e9e3 fix installdir in cmake build 2025-04-21 08:56:30 -07:00
jhlee525 0e22432045 change cmake_source_dir to project_source_dir so that FetchContent can work from other libraries 2025-04-15 12:49:56 -07:00
Thiago Macieira 45e4641059 Fix build with GCC < 11: [[fallthrough]] is supported but not allowed in C
I think GCC says `__has_cpp_attribute(fallthrough)` is true because C++
supports it (it means "has C++ attribute"), but that doesn't apply to
the C language. This causes a compilation error:

```
compilersupport_p.h:57:41: error: expected expression before '[' token
   57 | #    define CBOR_FALLTHROUGH            [[fallthrough]]
      |                                         ^
cborparser.c:225:13: note: in expansion of macro 'CBOR_FALLTHROUGH'
  225 |             CBOR_FALLTHROUGH;
      |             ^~~~~~~~~~~~~~~~
```

Instead, we should use the C23 `__has_c_attribute` to detect the C
attribute.

Fixes #293.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-04-08 11:45:14 -07:00
Thiago Macieira 6d932c012e CMake: generate tinycbor-version.h
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 17:23:13 -07:00
Thiago Macieira 7b607eb5e8 Remove the old Makefile and qmake buildsystem files
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 17:14:19 -07:00
Thiago Macieira e0b0be8cf6 CI: add an ASan step and split the small-build from test runs
The static builds with -Os or -Oz are there only so we get the library
size in the output. For testing, let's compile as a shared library and
properly in debug mode.

For Linux, we've had Valgrind. For macOS, this is now an ASan build.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 17:04:11 -07:00
Thiago Macieira cb58547aa9 CI: enable -Werror
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 17:04:11 -07:00
Thiago Macieira b91dc8af00 CI: switch testing to using CMake
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 17:04:11 -07:00
Thiago Macieira 814b8eac22 CMake: add the other, Qt-based tests
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira ff33012d13 CMake: enable building the tools
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira a1f8bd274c CMake: reformat the file a little
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira 85835b68af CMake: enable compiler warnings for the library build
It's good practice, especially for C code.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira 060da248de CMake: link shared libraries with -z defs (a.k.a. --no-undefined)
Also a best practice from "How To Write Shared Libraries"[1]

[1] https://akkadia.org/drepper/dsohowto.pdf

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira c3f5fac2cf CMake: build the shared library using hidden visibility
Best practices from "How To Write Shared Libraries"[1]

[1] https://akkadia.org/drepper/dsohowto.pdf

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira 39be8706cc CMake: update to say version 7.0
TinyCBOR has been at version 0.x for 10 years. This commit declares we
are now quite mature. I'm skipping version 1.0 through 6.0 and simply
calling the 0.7.0 to be 7.0.

Since this is binary compatible with the 0.6 version, I'm keeping the
SONAME of the shared library simply "libtinycbor.so.0" (note: previous
SONAME was "libtinycbor.so.0.6").

The Makefile build is not updated.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira ca1b419b89 CMake: force building the library as C99
As the comment says, to ensure we don't accidentally use something from
a new edition of the language than C99. In 2015 I think compilers still
defaulted to C99, but now in 2025, they default to C17.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira 3444a63ea0 CMake: rename the CMake files to "TinyCBOR"
The library itself is still lowercased as libtinycbor.a, libtinycbor.so,
tinycbor.dll, etc.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira 09e80842a5 CMake: use BUILD_TESTING to control whether we shall enable testing
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira 52cc7153b4 CMake: add option() to control the main library features
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira 5132bd4554 Update to say version 0.7.0
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 16:43:48 -07:00
Thiago Macieira 0e2b305e3f tst_Encoder: stop using QVariant::Type in favour of QMetaType::Type
The former has been deprecated since Qt 6.0 and produces warnings when
used.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 15:21:51 -07:00
Thiago Macieira be2ff257e6 Add newline to tinycbor-export.h define CBOR_API
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-18 15:09:44 -07:00
Nicolas Iooss 4682eaa1e0 Pretty: ensure recursionsLeft is not zero before decrementing it
When value_to_pretty is called with recursionsLeft=0 and an Array or Map
type, container_to_pretty is called with recursionsLeft - 1 = -1. This
breaks the recursion limit check.

In practice, this can be triggered with a CBOR data containing 1023
Arrays, a Tag and many more Arrays:

    $ python3 -c 'import sys;sys.stdout.buffer.write(b"\x9f" * 1023 + b"\xc0\x9f" + b"\x9f" * 100000 + b"\xff" * 101024)' | ./bin/cbordump
    Segmentation fault (core dumped)

This segmentation fault is due to the stack growing too much, due to the
quantity of recursive calls.

Fix this by reporting a proper error when recursionsLeft <= 0, instead
of when recursionsLeft == 0. The same input now produces:

    [_ [_ [_ ... [_ 0([
    -: internal error: too many nested containers found in recursive function
    _ <nesting too deep, recursion stopped>

Moreover, using fewer nested arrays works fine:

    $ python3 -c 'import sys;sys.stdout.buffer.write(b"\x9f" * 1023 + b"\xc0\x9f" + b"\x9f" * 1024 + b"\xff" * 2048)' |./bin/cbordump
    [_ [_ [_ ... [_ 0([_ <nesting too deep, recursion stopped>])] ... ]]]

Also modify the test when formatting CborTagType to ensure
value_to_pretty is never called with a negative recursionsLeft.
2025-03-18 10:49:39 -07:00
Nicolas Iooss f96502575f Pretty: fix Undefined Behavior with NaN floats
When printing CBOR data containing NaN, function convertToUint64 does an
undefined behavior. This can be reproduced using test cases from
tests/parser/data.cpp:

    $ make CC='clang -fsanitize=undefined'
    $ printf "\xfb\x7f\xf8\0\0\0\0\0\0" | ./bin/cbordump
    src/cborpretty.c:171:17: runtime error: nan is outside the range of
    representable values of type 'unsigned long'
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/cborpretty.c:171:17 in
    nan

    $ printf "\xf9\x7e\x00" | ./bin/cbordump
    src/cborpretty.c:171:17: runtime error: nan is outside the range of
    representable values of type 'unsigned long'
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/cborpretty.c:171:17 in
    nan

Fix this by checking whether the value to convert is not NaN.
2025-03-18 08:07:34 -07:00
Thiago Macieira 8684cdef61 Move the source-selection macros to a common header
Avoids having to repeat ourselves.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-14 10:11:20 -07:00
Nicolas Iooss 6d31efad9a CBOR-to-JSON: fix integer overflow when computing allocation size
Use add_check_overflow and mul_check_overflow to ensure the arithmetic
operations do not overflow when computing the size to allocate.
2025-03-14 09:44:54 -07:00
Nicolas Iooss 628dee0d65 CBOR-to-JSON: fix memory leak when parsing invalid CBOR
When function text_string_to_escaped successfully parses a string and
fails to parse the next value (cbor_value_finish_string_iteration
returns an error), it correctly propagates the error but the string is
never freed.

This can be reproduced with:

    make CC='clang -g -fsanitize=address'
    printf '\x82\x60\xff' | ./bin/cbordump -j

clang's Address Sanitizer reports:

    =================================================================
    ==20317==ERROR: LeakSanitizer: detected memory leaks

    Direct leak of 1 byte(s) in 1 object(s) allocated from:
        #0 0x560b654b9916 in __interceptor_realloc (/tinycbor/bin/cbordump+0xa4916) (BuildId: f9933666b5d987b21f68c2887de4aebe93bc2bef)
        #1 0x560b654f5c18 in escape_text_string /tinycbor/src/cbortojson.c:331:15
        #2 0x560b654f3e29 in text_string_to_escaped /tinycbor/src/cbortojson.c:377:19
        #3 0x560b654f267d in value_to_json /tinycbor/src/cbortojson.c:674:19
        #4 0x560b654f34c2 in array_to_json /tinycbor/src/cbortojson.c:545:25
        #5 0x560b654f2085 in value_to_json /tinycbor/src/cbortojson.c:627:19
        #6 0x560b654f1baf in cbor_value_to_json_advance /tinycbor/src/cbortojson.c:816:12
        #7 0x560b654ea928 in dumpFile /tinycbor/tools/cbordump/cbordump.c:76:19
        #8 0x560b654ead2b in main /tinycbor/tools/cbordump/cbordump.c:149:9
        #9 0x7fa9d7629d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

    SUMMARY: AddressSanitizer: 1 byte(s) leaked in 1 allocation(s).

Fix this by freeing the string when cbor_value_finish_string_iteration
fails.

Fixes: e072bc1d78 ("CBOR-to-JSON: do properly escape JSON strings")
2025-03-14 09:31:03 -07:00
Thiago Macieira d0a6def07b CBOR-to-JSON: fix UB in converting out-of-bounds FP to integer
Both the C and C++ standards say it is Undefined Behavior to convert a
floating point number to integer if the input is out of bounds of the
destination type.

And indeed this started failing in recent builds, with
  val = 18446744073709551616  (2^64)
it has probably been producing ival = 18446744073709551615 for a while,
but the conversion back to floating point now rounded up and compared
equal to the input.

So let's just fix it.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-12 19:35:44 -07:00
Thiago Macieira c52d731e5e cbor.h: let tinycbor-export.h define CBOR_API
And make CBOR_PRIVATE_API fall back to it.

We also need to provide the file for the old Makefile build too.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-12 16:49:17 -07:00
Thiago Macieira 582423c9d9 CMake: Fix build: add new sources since CMakeLists.txt was created 2025-03-12 16:49:17 -07:00
Samuel Debionne 37d1a6dee3 Add CMake support 2025-03-12 16:49:17 -07:00
Thiago Macieira c60b710ff0 tst_Parser: fix build: define CBOR_PARSER_MAX_RECURSIONS
I don't know how this was compiling.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-12 15:39:02 -07:00
Thiago Macieira 4050fa58c2 tst_Parser: add some testing rows for floating point data
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-12 15:21:35 -07:00
Thiago Macieira 1577f3b538 tst_ToJson: add a test for the ExpectedBase64url tag too
Just to confirm it works.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-12 15:21:35 -07:00
Thiago Macieira 0f3008d54c AppVeyor: replace Qt 5.13+MSVC 2017 with Qt 6.8+MSVC 2022
MSVC 2017 is way too old now.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-12 15:21:35 -07:00
Thiago Macieira 6e3333ebe0 Encoder: add unit test for cbor_encode_raw
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-12 15:21:35 -07:00
TSonono abb7b3fe35 Added the method cbor_encode_raw to the API
This method allows for writing raw data directly to the encoding buffer. This can be useful if you have something stored as CBOR encoded data.

Fixes #162.

Signed-off-by: Tofik Sonono <tofiksonono@msn.com>
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-12 15:21:35 -07:00
Thiago Macieira c0aad2fb21 cborparser_dup_string: don't modify *buffer until success
We were returning from the function with the memory we had allocated and
freed, if the second iteration over the string produced a failure that
didn't happen on the first one. This can't happen with pure memory
buffers, but can happen with an external data source that fails to
produce the same contents twice.

I'm documenting that the values in all error conditions except for OOM
are undefined, so one mustn't attempt to use them, even to free. This
does not change behaviour of the library, just documents.

But this commit does make it clear the OOM condition will return a valid
`*buflen` and `next`, the latter of which is new behaviour with this
commit.

Fixes #258.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
v0.6.1
2025-03-11 18:32:15 -07:00
Thiago Macieira 2fc4c35f9d json2cbor: don't use the buffer variable after realloc()
There's a discussion in the C and C++ communities whether you're allowed
to use the values of pointers that have been deallocated, if you don't
dereference them. Some argue that it is Undefined Behaviour in spite of
the numeric value stored in the variable not having changed.

Instead of arguing, let's just make sure we don't use the pointers after
they have become dangling. We only needed the offset of how far we've
written into the buffer to restore the state and we have a function that
returns exactly that.

Seen while debugging #259.

Drive-by keep the `buffersize` global variable unchanged until after
`realloc()` has returned with success.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-11 18:24:45 -07:00