Commit Graph

718 Commits

Author SHA1 Message Date
ToruNiina
a76c5b385f refactor: replace mut-ref by take-and-move pattern 2024-03-20 22:40:46 +09:00
ToruNiina
cc2e453b5b feat: remove strerror from errmsg in FILE* version
parse(FILE*) is a minor overload, but dispatching strerror takes too
much cost. Standard library version is not thread-safe, so some compiler
reports a warning. There are thread-safe versions defined in XSI, GNU,
and Windows. XSI/GNU versions can be detected by macros, but in some
cases, detection-by-macro written in the doc does not work.
Since errno can be obtained from the exception, users can call strerror
that is available in their env if needed. We can just report errno.
2024-01-07 10:48:17 +09:00
Toru Niina
1bb5284eb5
Merge pull request #233 from Esonhugh/master
fix issue #231
2024-01-05 01:39:23 +09:00
ToruNiina
1c2c710ec9 refactor: add comment 2024-01-04 21:18:47 +09:00
ToruNiina
8e214ec411 fix: limit value recursion in array/inl-table
to avoid parse_value/parse_array recursion
2024-01-04 20:45:35 +09:00
Esonhugh
0db935b602 fix: let __DARWIN_C as an exception 2023-12-27 11:19:59 +08:00
Esonhugh
ed577df40a fix: strerror_r error handling toml/exception.hpp in macos 2023-12-27 11:11:23 +08:00
ToruNiina
937a7c45fe feat: fill char buffer with null char
those funcs always return null-terminated string but just to make it
sure
2023-10-11 01:44:30 +09:00
ToruNiina
947c995189 fix: include array to use char buffer 2023-10-11 01:43:55 +09:00
ToruNiina
9b7b8908e8 fix: avoid evaluating undefined macro as zero
to suppress a warning
2023-10-11 01:24:41 +09:00
ToruNiina
22d22198ec feat: use thread-safe variant of strerror 2023-10-11 01:08:12 +09:00
ToruNiina
dfc625f38d fix: #229 do not move temporary object 2023-10-10 23:49:51 +09:00
Martin Tournoij
d2937ff4e1
Escape control characters in keys
Previously a key like:

        "a\u0000\u0001b" = 1

Would get written with literal control characters, rather than escapes:

        "a<00><01>b" = 1

The "valid/key/quoted-unicode" test from toml-test would fail with this,
although it seems they're not run automatically(?)

Can also reproduce with something like:

        % cat test.cpp
        #include <toml.hpp>
        #include <iostream>

        int main()
        {
                const auto data  = toml::parse("test.toml");
                std::cout << data << "\n";
                return 0;
        }

        % cat test.toml
        "a\u0000\u0001b" = "a\u0000\u0001b"

        % c++ -I. test.cpp

        % ./a.out
        "ab" = "a\u0000\u0001b"

        % ./a.out | hexdump -C
        00000000  22 61 00 01 62 22 20 3d  20 22 61 5c 75 30 30 30  |"a..b" = "a\u000|
        00000010  30 5c 75 30 30 30 31 62  22 0a 0a                 |0\u0001b"..|
2023-10-10 09:03:33 +01:00
ToruNiina
327f6e7701 fix: set locale to C when writing numbers 2023-05-28 18:42:33 +09:00
ToruNiina
e36eabf216 feat: add get<T> overload with toml::value& 2023-05-27 00:16:17 +09:00
Sergey Vidyuk
78ae165096 Fix for case when vector iterator is raw pointer
We are using patched libc++ which uses raw pointers for vector itrators
to improve code compilation speed. This commit fixed two compilation
issues in toml11:
 * location::const_iterator deinition assumes that vector const_iterator
   is struct or class type rather than raw pointer.
 * `const const_itetr foo()` triggers `-Wignored-qualifiers` for primitive
   types and `void` which breaks `-Wextra -Werror` compilation.
2023-03-16 23:02:19 +07:00
ToruNiina
ce941c318b fix: prevent windows minmax macro 2023-02-12 20:27:14 +09:00
ToruNiina
51587338cd fix: avoid overflow at postproc of the last loop 2023-02-12 18:50:46 +09:00
ToruNiina
418bfe9117 fix: cast explicitly to avoid un/signed comparison 2023-02-12 16:44:18 +09:00
ToruNiina
15346114ef fix: allow long binary integer and leading zeros 2023-02-12 16:22:23 +09:00
ToruNiina
f9b224c222 fix: reopening table implicitly defined by aot 2023-02-12 02:55:03 +09:00
Toru Niina
75daa2dde0
Merge pull request #208 from cxw42/issue199-nonutf-string
Fix address-sanitizer error when parsing literal strings having invalid UTF-8 characters
2023-02-11 23:46:53 +09:00
Chris White
a2f884b11e fix: parse_ml_literal_string() properly issues invalid-utf8 errors
Fix the same out-of-bounds read as in parse_literal_string().
2023-01-14 18:32:40 -05:00
Chris White
e3639d2bbc fix: parse_literal_string() properly issues invalid-utf8 errors
When creating the inner iterator, make sure it points into the same
vector as the outer iterator.  Otherwise, attempts to reset the iterator
wind up causing it to read out-of-bounds.

Fixes #199.
2023-01-14 18:32:40 -05:00
Chris White
626b0e6b95 fix: Correct function names in error messages
- fix error messages that referred to the wrong functions.
- parse_key(): remove "detail::" from the only error message that had
  it, for consistency with the other error messages in that function
2023-01-14 18:24:17 -05:00
Moritz Klammler
e064a5c371 Avoid unnecessary copies of parser result
The 'result' class has unwrap() and unwrap_err() member functions
overloaded for const lvalue and rvalue *this to avoid an unnecessarily
copying the to-be unwrapped object of its containing object is going to
be discarded anyway.  Alas, the parse() function toml/parser.hpp file
stored the parse result in a local `const` variable so, although the
unwrap call would have been the last use of the object in each case, the
unnecessary copy would still be made.  This patch removes the `const`
and adds a std::move() to actually benefit from the already implemented
optimization.
2022-09-29 19:02:52 +02:00
Moritz Klammler
cf8a977be2 Don't deliberately dereference the null pointer
This patch addresses a static analysis issue reported by Cppcheck 2.9
where several member functions of the toml::discard_comment class
defined in the toml/comments.hpp header were implemented to deliberately
dereference the null pointer returned unconditionally by the
always-empty container's data() member function.  This behavior wasn't
technically wrong because those functions all have as precondition that
the container is non-empty so they must never be called on an instance
of toml::discard_comment but we can still be more helpful without
adversely affecting code generation.  Instead of dereferencing the null
pointer, this patch has these functions call an inline private helper
function which is defined to invoke __builtin_unreachable() if available
"and then" throw an exception with a helpful error message.  Even at the
-O1 level, GCC will optimize the code under the assumption that the
function will never be called (i.e. no assembly is emitted), making
failure to ensure this undefined behavior exactly as if the null pointer
had been dereferenced.  However, static analysis will now understand the
programmer's intent and remain silent.  Furthermore, when using the -O0
or -Og levels, GCC won't optimize under this assumption so the exception
will be thrown and might be helpful for debugging.  Compilers that don't
have __builtin_unreachable() won't get any help in determining that the
function must not be called and will have to figure this out by
analyzing the calling code -- which really shouldn't exist in the first
place anyway as the whole point is that these functions must not be
called.
2022-09-29 17:59:28 +02:00
Moritz Klammler
8bb2c63a01 Don't compare iterators from potentially different containers
This patch addresses a static analysis issue reported by Cppcheck 2.9
where an assertion in the toml/region.hpp header would compare two
container's (that are known to be of type std::vector<char>) begin() and
end() iterators in order to verify that they are the same.  This
assertion either passes or invokes undefined behavior.  Which isn't
technically wrong because calling code must always ensure that
preconditions are met and assertions therefore pass anyway but it does
make the value that is added by having the assertion in the first place
marginal.  Fortunately, the assertion was easy to rewrite: Just compare
the container's address itself.  This is well-defined regardless of
whether the assertion will pass or fail.
2022-09-29 17:59:28 +02:00
Moritz Klammler
79c125b54f Initialize data members for defaulted c'tors
This patch addresses a static analysis issue reported by Cppcheck 2.9
where several classes in the toml/datetime.hpp header explicitly default
all their special member functions, including the default constructor,
but don't provide initializers for their data members.  This might or
might not have caused any observable surprising behavior but I agree
with Cppcheck on this one in that an explicitly defaulted default
constructor should be expected to initialize all data members.  So let's
do that.
2022-09-29 17:59:27 +02:00
ToruNiina
b02aaed4fc fix: use fs::path, not fs::path::string() result 2022-09-29 21:28:35 +09:00
ToruNiina
c2af975609 fix: add missing include file and specifiers 2022-09-29 20:14:58 +09:00
Toru Niina
c3a3423fb2
Merge pull request #193 from lukash/replace-fstream
Use C-style IO instead of ifstream for parsing
2022-09-24 00:17:52 +09:00
Lukáš Hrázký
6c2c804eff fix: Improve error handling of ifstream a bit
Set the exceptions mask so that exceptions are thrown when an I/O error
occurs. Also throw the same exception type when the opening fails.
2022-09-08 17:05:15 +02:00
Lukáš Hrázký
021d84623c chore: De-duplicate code for parse(std::filesystem::path) 2022-09-08 17:05:15 +02:00
Lukáš Hrázký
bf9c9d620d feat: Add a parse(FILE *) interface
The fstream classes are notorious for their non-existent error handling.

This adds a C-style fILE * IO (fopen(), etc.) alternative interface, so
that if a user needs reliable error handling, they can use that, albeit
more inconvenient, but more robust approach.
2022-09-08 17:05:11 +02:00
Jajauma's GitHub
72af7b48d3 Avoid possible lexer truncation warnings
Instead of static_cast calls that convert int to char, literals of type
char are now used directly with the value encoded via escape sequence.

The benefits are:
- code without static_cast is much more compact and expresses intent
better
- fixed value truncation warning on some compilers (e.g. C4309 on Visual
Studio 2017)
2022-08-13 10:30:18 +00:00
ToruNiina
1400dd223f fix: #192 quick fix by checking address 2022-07-01 01:09:22 +09:00
Lukáš Hrázký
594accf9a7 chore: Don't include fstream in lexer 2022-06-30 11:37:53 +02:00
ToruNiina
8e95891af1 fix: report an error if a table is inserted to aot 2022-06-22 21:48:37 +09:00
ToruNiina
1ba3be38d0 fix: point the beginning of value in err msg 2022-06-08 01:33:10 +09:00
ToruNiina
12d0dbc6f4 fix: throw if number cannot be read losslessly
it throws when strtoll and strtof that are internally called fail
2022-06-08 00:39:04 +09:00
Giel van Schijndel
aff6f0f574 fix: don't force a copy of std::string fname when moving is an option
Taking this parameter by const reference forces us to copy it (because
we know we're going to store it). Taking it by r-value reference would
suggest that we _might_ take ownership over it and would also force the
user to make a copy if they wish to retain the original value.

Taking this parameter by value however clearly gives us ownership of its
content without forcing a copy if it's implicit conversion from
`const char*` or explicitly handed over to us by the user via std::move.
2022-06-02 14:39:10 +02:00
ToruNiina
25be97dc39 refactor: style update 2022-05-29 00:37:39 +09:00
Ken Matsui
9086b1114f
Support changing color mode at runtime 2022-05-27 00:01:28 +09:00
Ken Matsui
c26aa013cd
Support opting out of the default [error] prefix 2022-05-22 13:50:45 +09:00
ToruNiina
dde351ea40 feat: add escape sequence of ESC
as an unreleased feature
2022-03-16 22:39:52 +09:00
ToruNiina
97cc0ef62b fix #166: reorder local/gmtime wrapper for MSVC 2022-03-13 00:20:00 +09:00
ToruNiina
0e80cabe65 Merge branch 'check-specialized-conversion' 2022-03-12 19:00:29 +09:00
ToruNiina
e60442c6db fix: check if re-open dotkey by a table
like:
```
a.b.c = "foo"
[a.b] # this is invalid
d = "bar"
```
2022-03-12 18:22:01 +09:00
ToruNiina
03259e2003 fix #177: check specific conversion function
when converting toml::value to array-like
2021-12-25 14:08:55 +09:00