Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cwe_checker
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-depend
cwe_checker
Commits
ad8a61f7
Unverified
Commit
ad8a61f7
authored
Nov 14, 2022
by
Enkelmann
Committed by
GitHub
Nov 14, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix format string parsing bug (#359)
parent
a16e6589
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
1 deletions
+36
-1
arguments.rs
src/cwe_checker_lib/src/utils/arguments.rs
+8
-1
tests.rs
src/cwe_checker_lib/src/utils/arguments/tests.rs
+28
-0
No files found.
src/cwe_checker_lib/src/utils/arguments.rs
View file @
ad8a61f7
...
...
@@ -57,7 +57,14 @@ pub fn parse_format_string_parameters(
format_string
:
&
str
,
datatype_properties
:
&
DatatypeProperties
,
)
->
Result
<
Vec
<
(
Datatype
,
ByteSize
)
>
,
Error
>
{
let
re
=
Regex
::
new
(
r
#
"
%\
d{0,2}(([c,C,d,i,o,u,x,X,e,E,f,F,g,G,a,A,n,p,s,S])|(hi|hd|hu|li|ld|lu|lli|lld|llu|lf|lg|le|la|lF|lG|lE|lA|Lf|Lg|Le|La|LF|LG|LE|LA))"
#
)
// Regex parts:
// - `%` starts a format string parameter
// - `[+\-#0]{0,1}` matches the flags `+`, `-`, `#`, `0` if present (for printf-like functions)
// - `\d*` matches the width parameter
// - `[\.]?\d*` matches the precision parameter (for printf-like functions)
// - `[cCdiouxXeEfFgGaAnpsS]` matches a format specifier without length parameter.
// - `hi|hd|hu|li|ld|lu|lli|lld|llu|lf|lg|le|la|lF|lG|lE|lA|Lf|Lg|Le|La|LF|LG|LE|LA` matches format specifiers with length parameter.
let
re
=
Regex
::
new
(
r
#
"
%
[+
\
-#0]{0,1}
\
d*[
\
.]?
\
d*([cCdiouxXeEfFgGaAnpsS]|hi|hd|hu|li|ld|lu|lli|lld|llu|lf|lg|le|la|lF|lG|lE|lA|Lf|Lg|Le|La|LF|LG|LE|LA)"
#
)
.expect
(
"No valid regex!"
);
let
datatype_map
:
Vec
<
(
Datatype
,
ByteSize
)
>
=
re
...
...
src/cwe_checker_lib/src/utils/arguments/tests.rs
View file @
ad8a61f7
...
...
@@ -96,6 +96,34 @@ fn test_parse_format_string_destination_and_return_content() {
);
}
/// Test the regular expression used for format string parameter parsing on specific cases.
#[test]
fn
test_format_string_regex
()
{
let
regex
=
Regex
::
new
(
r
#
"
%
[+
\
-#0]{0,1}
\
d*[
\
.]?
\
d*([cCdiouxXeEfFgGaAnpsS]|hi|hd|hu|li|ld|lu|lli|lld|llu|lf|lg|le|la|lF|lG|lE|lA|Lf|Lg|Le|La|LF|LG|LE|LA)"
#
)
.expect
(
"No valid regex!"
);
let
format_string
=
"one
%s
, two
%
.2lf
%%
, three
%
.2lf
%%
"
;
let
results
:
Vec
<
_
>
=
regex
.captures_iter
(
format_string
)
.map
(|
cap
|
cap
[
1
]
.to_string
())
.collect
();
assert_eq!
(
&
results
[
..
],
vec!
[
"s"
,
"lf"
,
"lf"
]);
let
format_string
=
"test
%+2.300f
,
%-2.300f%#2.300f%02.300f
"
;
let
results
:
Vec
<
_
>
=
regex
.captures_iter
(
format_string
)
.map
(|
cap
|
cap
[
1
]
.to_string
())
.collect
();
assert_eq!
(
&
results
[
..
],
vec!
[
"f"
,
"f"
,
"f"
,
"f"
]);
let
format_string
=
r
#
"
%c
Cd
%s
s
%256s
/dev/bus/usb/
%03d
/
%s
"
#
;
let
results
:
Vec
<
_
>
=
regex
.captures_iter
(
format_string
)
.map
(|
cap
|
cap
[
1
]
.to_string
())
.collect
();
assert_eq!
(
&
results
[
..
],
vec!
[
"c"
,
"s"
,
"s"
,
"d"
,
"s"
]);
}
#[test]
fn
test_parse_format_string_parameters
()
{
let
test_cases
:
Vec
<&
str
>
=
vec!
[
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment