Commit Graph

12 Commits

Author SHA1 Message Date
Caleb Connolly
4574736afc parser: handle decimal '0' when parsing numbers
The commit 61f6fe9d1c ("parser: be more restrictive when parsing numbers")
introduced a bug where having a single '0' would cause a parser error
due to the base handling logic swallowing the ;. Fix this
and add a test to check for it.
2022-07-18 15:48:00 -05:00
Alex Elder
e61ff7276e parser: fix another test file
In "tests/num_large.qmi", the same name is used twice for constant
symbols, which results in a parse error.  Fix this test file bug.

Fixes: ad48050 ("parser: properly support 64-bit numbers")
Signed-off-by: Alex Elder <elder@linaro.org>
2021-10-08 17:05:42 -05:00
Alex Elder
de50c02678 parser: don't treat 8 as a valid octal digit
The function isodigit() was defined to mimic isxdigit(), indicating
whether a given character was an octal digit.  But as written, it
considers '8' to be a valid octal digit, which it is not.  Fix this
bug.

In addition, the "bad_octal.qmi" test file refers to an undefined
"test_struct" as a message member type.  Fix that by using u32
instead.

Fixes: 61f6fe9 ("parser: be more restrictive when parsing numbers")
Signed-off-by: Alex Elder <elder@linaro.org>
2021-10-08 17:01:32 -05:00
Alex Elder
ed896c97dc parser: add support for constant value substitution
Register symbolic constants as defined symbols when they are
defined.  When a constant symbol reference occurs after it's
been defined, the parsed token is modified to be a number type,
whose value is the value constant symbol.

One difference between a "normal" number token and a "constant"
number token is that the the string in a constant token contains
a copy of the symbolic name, whereas its a null pointer for a
"normal" number.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-35-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 14:19:51 -05:00
Alex Elder
789c4e9d23 parser: avoid token buffer overflow
Define TOKEN_BUF_SIZE as the size of the buffer used when parsing
tokens.  Define TOKEN_BUF_MIN as the minimum size of the token
buffer; the size comes from what's necessary to represent a maximal
64-bit octal value.

Add checks in yylex() to avoid exhausting the token buffer on
pathological input.  Use the minimum buffer size to NUL-terminate
the buffer for a message if the token name is too long.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-28-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:35:07 -05:00
Alex Elder
72d1687658 parser: disallow duplicate members
Check each constant as it is recognized to ensure its name does not
duplicate an already-defined constant.

Check each message member as it is recognized to ensure its name
does not duplicate an already-defined member.  Also check its id
value to ensure the same value isn't used more than once.

Check each struct member as it is recognized to ensure its name does
not duplicate an already-defined member.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-27-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:34:46 -05:00
Alex Elder
83931b750f parser: introduce qmi_package_parse()
Rename parse_package() to be qmi_package_parse(), and have it assign
the qmi_package pointer internally rather returning it.  This makes
the function name match the pattern used for all other production
rules.

The one caller of qmi_package_parse() assumes the package name has
been specified.  When parsing has completed, check to ensure the
package is specified, and only allow it to be specified once.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-26-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:32:25 -05:00
Alex Elder
61f6fe9d1c parser: be more restrictive when parsing numbers
When a number is parsed, the leading one or two characters are used
to indicate whether the number should be interpreted as hexadecimal,
octal or decimal.

But because the parser accepts any digits regardless of the base, it
allows things like 039 to be treated as an octal number, despite '9'
not being a valid digit.  The previous commit makes matters even
worse, allowing [a-fA-F] to be accepted for octal or decimal values.
Such errors are caught (but ignored) later when converting the
accepted string into a number in strtoull().

We are already looking at the first character or two to determine
the base, *after* scanning the number.  Instead, determine the base
when the first one or two characters are first input, and restrict
which characters are accepted in the number based on that.

As a consequence, strtoul() will examine all of the characters
comprising the number (whereas previously it would stop if it
encountered invalid character for the base).

Finally, accept either "0x" or "0X" to indicate hexadecimal.

This doesn't actually change behavior much, but as long as we're
checking every character in a number for validity we might as well
be restrictive.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-25-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:32:01 -05:00
Alex Elder
84f213e584 parser: properly support hexadecimal numbers
In yylex(), when parsing a number, an attempt is made to allow
hexadecimal values to be interpreted if the second character of the
token being parsed is 'x'.  However the parser is only otherwise
permitting digits 0-9 to be used.  Fix this by using isxdigit()
rather than isdigit().

The result still permits nonsensical values to be accepted, but at
least hexidecimal values containing [a-fA-F] will be interpreted
correctly.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-24-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:30:21 -05:00
Alex Elder
ad480502eb parser: properly support 64-bit numbers
The language supports specifying 64-bit unsigned values, but the num
field of the token structure is not guaranteed to be 64 bits wide.
Change its type to be unsigned long long, and change the code that
parses numbers to use strtoull() so we can actually accept a 64-bit
value.

This is also true of the value field of the token type.  Change it
to unsigned long long as well (and format it as unsigned).

Check the return value of strtoull(), and if the result is out of
range, report an error.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-23-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:29:38 -05:00
Alex Elder
26191dc4a9 parser: reset fixed flag each for each array
If a message contains an array, we need to reset the flag that
indicates whether it is a fixed array or not each time through the
loop parsing message members.  Otherwise a non-fixed array declared
after a fixed array will be marked as fixed.

Drop the "int" in the definition of the array_size local varaible in
qmi_message_parse() to be consistent with the rest of the program.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-22-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:28:56 -05:00
Alex Elder
69f032f380 parser: add support for comments
Add the ability for input files to have comments in them.  A comment
begins with a '#' character and continues through the end of the line.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-21-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:28:05 -05:00