Skip to content

Commit

Permalink
lib/addoninfo.cpp: Add JSON schema validation.
Browse files Browse the repository at this point in the history
For JSON schema validation in cpp code, valijson
(https://github.com/tristanpenman/valijson) is added. It supports picojson and
is actively maintained. It is used in one-header-include style, because it
seems the simplest solution, for now at least. The header is added, alongside
the LICENSE file, in externals/valijson/.

Various warnings that are emitted compiling the default code generated by
valijson are fixed; these fixes are pushed upstream.

As JSON is now validated against a schema, various ad-hoc checks could be
safely removed.

A simple way to test addon JSON schema validation, is to invoke cppcheck using:

   cppcheck --addon='{"script":1}' test.c

A minor bugfix is that the file specified by "executable" was searched using
getFullPath() with fileName as the second argument; it looks like this should
have been exename.

The fileName argument of parseAddonInfo() is only used for log messages, and is
now replaced by the string "inline JSON" instead of the actual JSON string that
was parsed. The command as given above will therefore output:

   Loading inline JSON failed. JSON schema validation failed: <root> [script]
   Value type not permitted by 'type' constraint., <root> Failed to validate
   against schema associated with property name 'script'.

The addon JSON unit tests are also updated and now operate on JSON specified on
the command line.
  • Loading branch information
mvds00 committed Jan 7, 2024
1 parent 8ebd9bb commit 022a7a8
Show file tree
Hide file tree
Showing 18 changed files with 10,908 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI-unixish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ jobs:
- name: Check syntax with NONNEG
run: |
ls lib/*.cpp | xargs -n 1 -P $(nproc) g++ -fsyntax-only -std=c++0x -Ilib -Iexternals -Iexternals/picojson -Iexternals/simplecpp -Iexternals/tinyxml2 -DNONNEG
ls lib/*.cpp | xargs -n 1 -P $(nproc) g++ -fsyntax-only -std=c++0x -Ilib -Iexternals -Iexternals/picojson -Iexternals/valijson -Iexternals/simplecpp -Iexternals/tinyxml2 -DNONNEG
build_qmake:

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ ifndef PREFIX
endif

ifndef INCLUDE_FOR_LIB
INCLUDE_FOR_LIB=-Ilib -isystem externals -isystem externals/picojson -isystem externals/simplecpp -isystem externals/tinyxml2
INCLUDE_FOR_LIB=-Ilib -isystem externals -isystem externals/picojson -isystem externals/valijson -isystem externals/simplecpp -isystem externals/tinyxml2
endif

ifndef INCLUDE_FOR_CLI
Expand Down
2 changes: 1 addition & 1 deletion createrelease
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# self check, fix critical issues:
# make clean && make CXXFLAGS=-O2 MATCHCOMPILER=yes -j4
# ./cppcheck -D__CPPCHECK__ -D__GNUC__ -DCHECK_INTERNAL -DHAVE_RULES --std=c++11 --library=cppcheck-lib --library=qt --enable=style --inconclusive --inline-suppr --suppress=bitwiseOnBoolean --suppress=shadowFunction --suppress=useStlAlgorithm --suppress=*:externals/picojson.h --suppress=functionConst --suppress=functionStatic --xml cli gui/*.cpp lib 2> selfcheck.xml
# ./cppcheck -D__CPPCHECK__ -D__GNUC__ -DCHECK_INTERNAL -DHAVE_RULES --std=c++11 --library=cppcheck-lib --library=qt --enable=style --inconclusive --inline-suppr --suppress=bitwiseOnBoolean --suppress=shadowFunction --suppress=useStlAlgorithm --suppress=*:externals/picojson.h --suppress=*:externals/valijson/valijson_picojson_bundled.hpp --suppress=functionConst --suppress=functionStatic --xml cli gui/*.cpp lib 2> selfcheck.xml
#
# Generate lib/checkers.cpp (TODO the premium checkers should not be statically coded)
# cd ~/cppchecksolutions/cppcheck && python3 tools/get_checkers.py > lib/checkers.cpp
Expand Down
2 changes: 2 additions & 0 deletions externals/externals.pri
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
INCLUDEPATH += $${PWD} \
$${PWD}/picojson \
$${PWD}/valijson \
$${PWD}/simplecpp \
$${PWD}/tinyxml2

HEADERS += $${PWD}/picojson/picojson.h \
$${PWD}/valijson/valijson_picojson_bundled.hpp \
$${PWD}/simplecpp/simplecpp.h \
$${PWD}/tinyxml2/tinyxml2.h

Expand Down
23 changes: 23 additions & 0 deletions externals/valijson/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (c) 2016, Tristan Penman
Copyright (c) 2016, Akamai Technologies, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit 022a7a8

Please sign in to comment.