Commit Graph
389 Commits
Author SHA1 Message Date
Thiago Macieira e072bc1d78 CBOR-to-JSON: do properly escape JSON strings
We hadn't bothered, as this was just example-like code to show how one
could convert from CBOR to JSON. But as it was added to the library (no
extra dependency), we should Do The Right Thing (DTRT) and escape.

This patch could have used cbor_value_to_pretty() to print the string,
which has better support for UTF-8 escaping and thus checks for UTF-8
correctness, but that would make map_to_json()'s metadata functionality
much more complex, especially since we cannot rely on open_memstream()
always being available. Therefore, we are partially duplicating
cborpretty.c's utf8EscapedDump().

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-11 14:42:41 -07:00
Thiago Macieira e6924451a9 CBOR-to-JSON: Limit how deep we process nested containers
1024 levels will probably be good enough for everyone, like
cborparser.c. For those for whom it isn't, they can set the limit during
the build.

We already had this for the plain parser, so TinyCBOR wouldn't cause a
stack overflow in case of a malformed stream (intentionally or not) when
simply parsing and advancing over the stream. This same protection
wasn't applied to the content converting from CBOR to JSON.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-03-11 10:55:26 -07:00
Marc Mutz 5bdc6ea3fe Always init CborEncoder::data in cbor_encoder_init_writer()
Coverity complains that, when CBOR_ENCODER_WRITE_FUNCTION is defined,
enc.data is read in cbor_encoder_create_map() when
cbor_encoder_init_writer() didn't write to it.

While 'data' is merely copied in cbor_encoder_create_map(), Coverity
is right, though: reading an uninitialized value is UB.

Fix by setting data.writer to nullptr (abstracting the difference
between C and C++ behind a new macro).
2025-03-10 13:00:39 -07:00
Thiago Macieira 53ff130af9 tst_Parser: replace Q_ASSERT(false) with Q_UNREACHABLE()
`Q_ASSERT()` disappears in release mode, leading Clang to print a static
analysis warning about an impossible condition:
```
tst_parser.cpp:251:16: warning: variable 'err' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
  251 |     } else if (ourType == CborTextStringType) {
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tst_parser.cpp:263:12: note: uninitialized use occurs here
  263 |     return err;
      |            ^~~
tst_parser.cpp:251:12: note: remove the 'if' if its condition is always true
```

`Q_UNREACHABLE()` becomes a plain `__builtin_unreachable()` in release
mode.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-02-13 17:19:28 -08:00
Thiago Macieira 92a02529d2 compilersupport_p.h: add a macro for the fallthrough attribute
The /* fallthrough */ comment isn't always handled, causing some
compilers to complain about falling through.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-02-10 14:37:44 -08:00
Thiago Macieira b2dbc005c3 CI: Remove .travis.yml file
Travis CI doesn't work any more

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-02-08 09:35:31 -08:00
Thiago Macieira ba42254b01 Enable CI checking in the dev branch too
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2025-02-07 12:52:14 -08:00
linraymond2006 1bcde8771b Use cbor_value_get_next_byte instead of accessing structure directly
Signed-off-by: linraymond2006 <linraymond2006@gmail.com>
2025-01-22 09:22:10 -08:00
LinRaymond2006 aa86c9d08e Corrected syntax error in examples/simplereader.c 2025-01-22 09:22:10 -08:00
Giuseppe D'Angelo 2dde97f0db Prevent a -Wundef warning
Follow the same "pattern" as for the rest of the #if directive: first
test if a macro is defined, then test the value. Otherwise the code
triggers a -Wundef warning (e.g. when building in C++).
2025-01-21 09:34:31 -08:00
Thiago Macieira 26c63e3d59 CI: add 'permissions' token to the GitHub actions file
Intel says this is needed

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2024-05-13 13:27:59 -07:00
Robert Dower 7520449b79 add required SECURITY.md file for OSSF Scorecard compliance 2024-05-06 17:56:41 -07:00
Thiago Macieira 268a61ed17 Makefile: disable cJSON support when building without math support
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2024-05-01 18:35:40 -07:00
Thiago Macieira 64ce7adff0 CI: Run the configure step in verbose mode and print the config output
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2024-05-01 18:29:45 -07:00
Thiago Macieira ccdd9c1cf4 CI: unbreak macOS: need to have CXX set
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2024-05-01 18:29:45 -07:00
Thiago Macieira db9f29d499 CI/Makefile: do allow Qt 6
I don't think we need to worry about Qt 4 any more.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2024-05-01 18:29:45 -07:00
Thiago Macieira ffc3bd6f84 CI: remove Valgrind on macOS: it doesn't work
```
==21147== Valgrind: debuginfo reader: ensure_valid failed:
==21147== Valgrind:   during call to ML_(img_get)
==21147== Valgrind:   request for range [18446744069408125024, +16) exceeds
==21147== Valgrind:   valid image size of 140733057859584 for image:
==21147== Valgrind:   "/usr/local/Cellar/icu4c/74.2/lib/libicudata.74.2.dylib"
```

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2024-05-01 18:29:45 -07:00
Thiago Macieira 24de1b065d CI: Get Homebrew to use a bottle (precompiled) Qt
Homebrew no longer carries precompiled versions of Qt for macos-11, so
switch to macos-13, the last on x86 CPUs (so we can still run
Valgrind). It's also going away after the end of June.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2024-05-01 18:29:45 -07:00
Thiago Macieira 4df5e4f700 Tests: disable the C90 test
We don't support this any more.
```
../../src/cbor.h:255:69: error: '_Bool' is a C99 extension [-Werror,-Wc99-extensions]
CBOR_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
                                                                    ^
```

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2024-05-01 18:29:45 -07:00
lightyear15 b768196eb5 follow-up commit to 75eaa19
facilitate replacement of malloc/free functions

open_memstream functions are left out of this change as they require
other functions from stdlib.
2024-05-01 16:59:08 -07:00
Peter A. Jonsson 4dcad260cb CI: add Github CI 2024-05-01 16:43:17 -07:00
Peter A. Jonsson c8e12e9075 CI: add dependabot configuration
Dependabot can provide automatic pull requests
for things in the repository that should
be updated.

Example PR from dependabot against a repo owned
by the github organization:

https://github.com/github/opensource.guide/pull/2248

Documentation:

https://docs.github.com/en/code-security/dependabot/dependabot-security-updates/about-dependabot-security-updates
2024-05-01 16:43:17 -07:00
Thiago Macieira 0d5538277d Makefile: Fix build with Clang
It doesn't know -Wdiscarded-qualifiers. Reported by @pjonsson on #247.

I really need to switch to CMake - #242

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2023-11-26 17:27:09 -07:00
Martin Jansa fd2f6cf9a7 tinycbor: fix build with gcc-13
* fixes:
  http://errors.yoctoproject.org/Errors/Details/701753/

qtbase/6.5.0-r0/git/tests/auto/corelib/serialization/qcborstreamwriter/../../../../../src/3rdparty/tinycbor/tests/encoder/data.cpp:242:90: error: invalid user-defined conversion from 'float' to 'const qfloat16&' [-fpermissive]
  242 |     QTest::newRow("nan_f16") << raw("\xf9\x7e\0") << QVariant::fromValue<qfloat16>(myNaNf());
      |                                                                                    ~~~~~~^~
n file included from TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtCore/qmetatype.h:14,
                 from TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtTest/qtestcase.h:11,
                 from TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtTest/qtest.h:13,
                 from TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtTest/QTest:1,
                 from TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/git/tests/auto/corelib/serialization/qcborstreamwriter/tst_qcborstreamwriter.cpp:4:
TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtCore/qfloat16.h:81:22: note: candidate is: 'constexpr qfloat16::qfloat16(NativeType)' (near match)
   81 |     constexpr inline qfloat16(NativeType f) : f(f) {}
      |                      ^~~~~~~~
TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtCore/qfloat16.h:81:22: note:   conversion of argument 1 would be ill-formed:
TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/git/tests/auto/corelib/serialization/qcborstreamwriter/../../../../../src/3rdparty/tinycbor/tests/encoder/data.cpp:242:90: warning: converting to 'qfloat16::NativeType' {aka '_Float16'} from 'float' with greater conversion rank
  242 |     QTest::newRow("nan_f16") << raw("\xf9\x7e\0") << QVariant::fromValue<qfloat16>(myNaNf());
      |                                                                                    ~~~~~~^~
TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/git/tests/auto/corelib/serialization/qcborstreamwriter/../../../../../src/3rdparty/tinycbor/tests/encoder/data.cpp:242:90: warning: converting to 'qfloat16::NativeType' {aka '_Float16'} from 'float' with greater conversion rank
TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtCore/qfloat16.h:81:42: note:   initializing argument 1 of 'constexpr qfloat16::qfloat16(NativeType)'
   81 |     constexpr inline qfloat16(NativeType f) : f(f) {}
      |                               ~~~~~~~~~~~^
In file included from TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtCore/qmetaobject.h:9,
                 from TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtTest/qtestcase.h:12:
TOPDIR/tmp-glibc/work/core2-64-oe-linux/qtbase/6.5.0-r0/image/usr/include/QtCore/qvariant.h:435:43: note:   initializing argument 1 of 'static std::enable_if_t<(is_copy_constructible_v<T> && is_destructible_v<T>), QVariant> QVariant::fromValue(const T&) [with T = qfloat16; std::enable_if_t<(is_copy_constructible_v<T> && is_destructible_v<T>), QVariant> = QVariant]'
  435 |     static inline auto fromValue(const T &value)
      |                                  ~~~~~~~~~^~~~~

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2023-11-26 17:27:09 -07:00
Peter A. Jonsson 65a4147021 Fix Doxygen warnings
The \c and \b commands accept a single word,
and there are no \section1 or \list commands
according to the Doxygen manual.
2023-10-30 11:10:56 -07:00
Brian 8b3e97d60e Update .appveyor.yml from QT v6.1 --> v6.5
Looks like AppVeyor dropped support for v6.1 QT as shown in https://www.appveyor.com/docs/windows-images-software/

Update to latest QT version
2023-10-27 08:02:45 -07:00
Piotr Wierciński 04b306c447 Use internal linkage for data.cpp files
The Qt uses test batching and potentially encoder/data.cpp
and parser/data.cpp can end up in the same translation unit.
This can be problematic as they declare symbols with the
same names.
Change both files to use internal linkage in order to avoid
symbols clashing.
2023-08-14 07:58:57 -07:00
Marc Mutz aee4f97f52 tst_Encoder: port away from Q_FOREACH
Qt is defaulting to QT_NO_FOREACH these days, so make sure we
integrate nicely with downstream and fix the single Q_FOREACH/foreach
user, in tst_encoder.cpp.

Unfortunately, the container's initialization code doesn't exactly
lend itself to making the container const (not even IILE
(Immediately-Invoked Lambda Expression) would help here, due to the
interdependency with `len`), so the idiomatic solution would be to use
std::as_const()/qAsConst().

The former is available from C++17, which we don't require, yet, and
the latter is not available under QT_NO_AS_CONST (the default for Qt
these days), so grab the nettle and implement a t17::as_const() that
switches between a manual implementation of std::as_const and the real
thing, depending on __cpp_lib_as_const. The `t17` here mimicks the qNN
(q20::remove_cvref_t/q23::forward_like/etc) mechanism used in Qt
itself for backports, with s/q/t/ because ... _T_inyCbor.

The t17 implementation is local to tst_encoder.cpp, but can easily be
extracted into a separate header once more users emerge.
2023-08-07 09:08:56 -07:00
piotreklc60 0b2e66d276 Added possibility to include external config file.
Recently I was working on a project where I had to define CBOR_PARSER_ADVANCE_BYTES_FUNCTION and CBOR_PARSER_TRANSFER_STRING_FUNCTION macros. Unfortunately the library doesn't include any external config file and building system I am using provides only possibility to add simple preprocessor definitions (like definition = value) on building script level. Adding macros is not possible. For this reason I am asking if we can add above change - if I add simple definition CBOR_EXTERNAL_CFG by build system then cbor will require cbor_cfg.h file where I can add needed macros.
2023-04-28 08:33:00 -07:00
Kal Conley 3cba6b11aa Use _Float16 for half conversions if available
Implement support for half-precision floating-point conversions using
`_Float16` introduced in C11 extension ISO/IEC TS 18661-3.
2023-02-23 17:13:37 -08:00
Nicolas Chataing 2455693393 fix(pretty): advance to the end of the iterator if recursion limit was hit 2023-02-13 11:22:04 -08:00
Nicolas Iooss 9c57e53a2f Tools: fix JSON misspelling in cbordump
Signed-off-by: Nicolas Iooss <nicolas.iooss@ledger.fr>
2023-02-11 18:22:52 -08:00
Frederic-Philippe Metz cd3cfc3bc9 Add support for ICCARM 2022-10-03 09:42:44 -07:00
Andy Huffman 75eaa19ef1 Facilitate replacement of standard library memory management functions 2022-09-09 19:55:32 -07:00
Thiago Macieira e27261ed5e Parser/Validation: Add a test for good data
We had tests for cbor_validate* API for the errors it would find, but not
that it did correctly reported CborNoError where no errors were
expected.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2022-09-07 10:47:25 -07:00
Florian Warzecha 9e97d07819 fix(validation): check CompleteData after iteration
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2022-09-07 10:33:23 -07:00
Khyber Sen 590c28a004 Removed #define _POSIX_C_SOURCEs where #define _GNU_SOURCE was also defined, as _GNU_SOURCE implicitly defines _POSIX_C_SOURCE, but on BSD/macOS, _POSIX_C_SOURCE prevents non-POSIX extensions from being defined. 2022-06-28 09:49:22 -07:00
mkm 0a488a5714 use cbor_ntohll instead of ntohl directly 2022-06-07 19:52:22 -07:00
Hamilton Chapman 22c2980855 If the buffer pointer is a null pointer then don't apply an offset to it 2022-04-04 08:50:59 -07:00
Thiago Macieira 02a7efcf6f Makefile: add -Werror=discarded-qualifiers to the C flags
Because C allows this:

  void f(int *x);
  void g(const int *x)
  { f(x); }

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2022-01-25 08:07:09 -08:00
Fabrice Fontaine 5f26eaf8d7 Makefile: add DISABLE_WERROR
Allow the user to disable -Werror to avoid the following build failure
with gcc 4.8 raised since version 0.6.0 and
https://github.com/intel/tinycbor/commit/e2a4ed135c4d9101c4df83f2dd033cd249b6ef07:

/home/buildroot/autobuild/instance-1/output-1/host/bin/arm-none-linux-gnueabi-gcc -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -I./src -std=gnu99 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64  -Os -g0 -D_FORTIFY_SOURCE=1 -Werror=incompatible-pointer-types -Werror=implicit-function-declaration -Werror=int-conversion -fPIC  -c -o src/cborerrorstrings.pic.o src/cborerrorstrings.c
cc1: error: -Werror=incompatible-pointer-types: no option -Wincompatible-pointer-types
cc1: error: -Werror=int-conversion: no option -Wint-conversion

Fixes:
 - http://autobuild.buildroot.org/results/a9f/a9fe64c42bb96f9e7b4af3050464f6570c1c00fa

Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
2022-01-12 08:13:56 -08:00
Alfie Fresta 6176e0a28d Makefile: Use file name instead of file descriptor for .config target 2021-10-04 11:59:18 -07:00
Thiago Macieira 10f739921d .gitignore: ignore the c90 test too 2021-09-03 13:19:19 -07:00
Dmitry Shachnev d393c16f3e Parser: fix reading it->extra on big endian when bytesNeeded == 1
Signed-off-by: Dmitry Shachnev <mitya57@gmail.com>
v0.6-rc1 v0.6.0
2021-09-03 13:08:25 -07:00
Thiago Macieira 8adc3cf3f4 Parser: modify the zero-copy string API
Instead of just one function (_cbor_value_get_string_chunk), we now have
_cbor_value_begin_string_iteration, _cbor_value_finish_string_iteration,
_cbor_value_get_string_chunk_size, and _cbor_value_get_string_chunk.

The "begin" function positions the pointer at the first chunk. That's
what makes "get_size" possible, since it doesn't need to check for any
state. The "finish" funcntion allows the caller to distinguish an error
parsing the string from an error parsing the next value.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2021-09-03 13:08:25 -07:00
Thiago Macieira 5159ec39df Parser: add a way to parse the current element again
We need to re-parse if the input buffer was too short to read the
current element's information. When that happens, the current element
will be CborInvalidType, so we can't easily resume.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2021-09-03 13:08:25 -07:00
Thiago Macieira 87a7a9308c Move the testdata out to a separate .cpp so they can be reused
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2021-09-03 13:08:25 -07:00
Thiago Macieira e26ff9a2dc WIP Initial API for delegated streaming out
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2021-09-03 13:08:25 -07:00
Thiago Macieira 34c84520a7 WIP Initial API for delegated streaming in
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2021-09-03 13:08:25 -07:00
Thiago Macieira 95129b84ed Parser: let cbor_value_leave_container() consume the Break
Instead of consuming it at the end of the last element of a map or array
of unknown length. This allows us to obtain the pointer to or offset of
the Break byte.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2021-09-03 13:08:25 -07:00