Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
0ee05b90
Unverified
Commit
0ee05b90
authored
Jul 02, 2019
by
Joseph Noir
Committed by
GitHub
Jul 02, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #852
Fix integer overflow check in number parser
parents
5225289f
e020e694
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
122 additions
and
15 deletions
+122
-15
libcaf_core/caf/detail/parser/add_ascii.hpp
libcaf_core/caf/detail/parser/add_ascii.hpp
+22
-3
libcaf_core/caf/detail/parser/sub_ascii.hpp
libcaf_core/caf/detail/parser/sub_ascii.hpp
+22
-3
libcaf_core/test/ipv4_address.cpp
libcaf_core/test/ipv4_address.cpp
+28
-7
libcaf_core/test/read_number.cpp
libcaf_core/test/read_number.cpp
+50
-2
No files found.
libcaf_core/caf/detail/parser/add_ascii.hpp
View file @
0ee05b90
...
@@ -19,7 +19,10 @@
...
@@ -19,7 +19,10 @@
#pragma once
#pragma once
#include <limits>
#include "caf/detail/parser/ascii_to_int.hpp"
#include "caf/detail/parser/ascii_to_int.hpp"
#include "caf/detail/type_traits.hpp"
namespace
caf
{
namespace
caf
{
namespace
detail
{
namespace
detail
{
...
@@ -28,12 +31,28 @@ namespace parser {
...
@@ -28,12 +31,28 @@ namespace parser {
// Sum up integers when parsing positive integers.
// Sum up integers when parsing positive integers.
// @returns `false` on an overflow, otherwise `true`.
// @returns `false` on an overflow, otherwise `true`.
// @pre `isdigit(c) || (Base == 16 && isxdigit(c))`
// @pre `isdigit(c) || (Base == 16 && isxdigit(c))`
// @warning can leave `x` in an intermediate state when retuning `false`
template
<
int
Base
,
class
T
>
bool
add_ascii
(
T
&
x
,
char
c
,
enable_if_tt
<
std
::
is_integral
<
T
>
,
int
>
u
=
0
)
{
CAF_IGNORE_UNUSED
(
u
);
if
(
x
>
(
std
::
numeric_limits
<
T
>::
max
()
/
Base
))
return
false
;
x
*=
Base
;
ascii_to_int
<
Base
,
T
>
f
;
auto
y
=
f
(
c
);
if
(
x
>
(
std
::
numeric_limits
<
T
>::
max
()
-
y
))
return
false
;
x
+=
y
;
return
true
;
}
template
<
int
Base
,
class
T
>
template
<
int
Base
,
class
T
>
bool
add_ascii
(
T
&
x
,
char
c
)
{
bool
add_ascii
(
T
&
x
,
char
c
,
enable_if_tt
<
std
::
is_floating_point
<
T
>
,
int
>
u
=
0
)
{
CAF_IGNORE_UNUSED
(
u
);
ascii_to_int
<
Base
,
T
>
f
;
ascii_to_int
<
Base
,
T
>
f
;
auto
before
=
x
;
x
=
(
x
*
Base
)
+
f
(
c
);
x
=
(
x
*
Base
)
+
f
(
c
);
return
before
<=
x
;
return
true
;
}
}
}
// namespace parser
}
// namespace parser
...
...
libcaf_core/caf/detail/parser/sub_ascii.hpp
View file @
0ee05b90
...
@@ -19,7 +19,10 @@
...
@@ -19,7 +19,10 @@
#pragma once
#pragma once
#include <limits>
#include "caf/detail/parser/ascii_to_int.hpp"
#include "caf/detail/parser/ascii_to_int.hpp"
#include "caf/detail/type_traits.hpp"
namespace
caf
{
namespace
caf
{
namespace
detail
{
namespace
detail
{
...
@@ -28,12 +31,28 @@ namespace parser {
...
@@ -28,12 +31,28 @@ namespace parser {
// Subtracs integers when parsing negative integers.
// Subtracs integers when parsing negative integers.
// @returns `false` on an underflow, otherwise `true`.
// @returns `false` on an underflow, otherwise `true`.
// @pre `isdigit(c) || (Base == 16 && isxdigit(c))`
// @pre `isdigit(c) || (Base == 16 && isxdigit(c))`
// @warning can leave `x` in an intermediate state when retuning `false`
template
<
int
Base
,
class
T
>
bool
sub_ascii
(
T
&
x
,
char
c
,
enable_if_tt
<
std
::
is_integral
<
T
>
,
int
>
u
=
0
)
{
CAF_IGNORE_UNUSED
(
u
);
if
(
x
<
(
std
::
numeric_limits
<
T
>::
min
()
/
Base
))
return
false
;
x
*=
Base
;
ascii_to_int
<
Base
,
T
>
f
;
auto
y
=
f
(
c
);
if
(
x
<
(
std
::
numeric_limits
<
T
>::
min
()
+
y
))
return
false
;
x
-=
y
;
return
true
;
}
template
<
int
Base
,
class
T
>
template
<
int
Base
,
class
T
>
bool
sub_ascii
(
T
&
x
,
char
c
)
{
bool
sub_ascii
(
T
&
x
,
char
c
,
enable_if_tt
<
std
::
is_floating_point
<
T
>
,
int
>
u
=
0
)
{
CAF_IGNORE_UNUSED
(
u
);
ascii_to_int
<
Base
,
T
>
f
;
ascii_to_int
<
Base
,
T
>
f
;
auto
before
=
x
;
x
=
(
x
*
Base
)
-
f
(
c
);
x
=
(
x
*
Base
)
-
f
(
c
);
return
before
>=
x
;
return
true
;
}
}
}
// namespace parser
}
// namespace parser
...
...
libcaf_core/test/ipv4_address.cpp
View file @
0ee05b90
...
@@ -42,13 +42,34 @@ CAF_TEST(constructing) {
...
@@ -42,13 +42,34 @@ CAF_TEST(constructing) {
CAF_CHECK_EQUAL
(
zero
.
bits
(),
0u
);
CAF_CHECK_EQUAL
(
zero
.
bits
(),
0u
);
}
}
CAF_TEST
(
to
and
from
string
)
{
CAF_TEST
(
to
string
)
{
ipv4_address
x
;
CAF_CHECK_EQUAL
(
to_string
(
addr
(
255
,
255
,
255
,
255
)),
"255.255.255.255"
);
auto
err
=
parse
(
"255.255.255.255"
,
x
);
}
CAF_CHECK_EQUAL
(
err
,
pec
::
success
);
CAF_CHECK_EQUAL
(
x
.
bits
(),
0xFFFFFFFF
);
CAF_TEST
(
from
string
-
valid
inputs
)
{
CAF_CHECK_EQUAL
(
to_string
(
x
),
"255.255.255.255"
);
auto
from_string
=
[](
string_view
str
)
->
expected
<
ipv4_address
>
{
CAF_CHECK_EQUAL
(
x
,
addr
(
255
,
255
,
255
,
255
));
ipv4_address
result
;
if
(
auto
err
=
parse
(
str
,
result
))
return
err
;
return
result
;
};
CAF_CHECK_EQUAL
(
from_string
(
"136.12.12.12"
),
addr
(
136
,
12
,
12
,
12
));
CAF_CHECK_EQUAL
(
from_string
(
"255.255.255.255"
),
addr
(
255
,
255
,
255
,
255
));
}
CAF_TEST
(
from
string
-
invalid
inputs
)
{
auto
should_fail
=
[](
string_view
str
)
{
ipv4_address
result
;
auto
err
=
parse
(
str
,
result
);
if
(
!
err
)
CAF_ERROR
(
"error while parsing "
<<
str
<<
", expected an error but got: "
<<
to_string
(
result
));
};
should_fail
(
"256.12.12.12"
);
should_fail
(
"1136.12.12.12"
);
should_fail
(
"1137.12.12.12"
);
should_fail
(
"1279.12.12.12"
);
should_fail
(
"1280.12.12.12"
);
}
}
CAF_TEST
(
properties
)
{
CAF_TEST
(
properties
)
{
...
...
libcaf_core/test/read_number.cpp
View file @
0ee05b90
...
@@ -22,9 +22,12 @@
...
@@ -22,9 +22,12 @@
#include "caf/test/unit_test.hpp"
#include "caf/test/unit_test.hpp"
#include "caf/variant.hpp"
#include "caf/detail/parser/add_ascii.hpp"
#include "caf/detail/parser/read_number.hpp"
#include "caf/detail/parser/read_number.hpp"
#include "caf/detail/parser/sub_ascii.hpp"
#include "caf/expected.hpp"
#include "caf/pec.hpp"
#include "caf/variant.hpp"
using
namespace
caf
;
using
namespace
caf
;
...
@@ -96,6 +99,51 @@ struct fixture {
...
@@ -96,6 +99,51 @@ struct fixture {
CAF_TEST_FIXTURE_SCOPE
(
read_number_tests
,
fixture
)
CAF_TEST_FIXTURE_SCOPE
(
read_number_tests
,
fixture
)
CAF_TEST
(
add
ascii
-
unsigned
)
{
using
detail
::
parser
::
add_ascii
;
auto
rd
=
[](
string_view
str
)
->
expected
<
uint8_t
>
{
uint8_t
x
=
0
;
for
(
auto
c
:
str
)
if
(
!
add_ascii
<
10
>
(
x
,
c
))
return
pec
::
integer_overflow
;
return
x
;
};
for
(
int
i
=
0
;
i
<
256
;
++
i
)
CAF_CHECK_EQUAL
(
rd
(
std
::
to_string
(
i
)),
static_cast
<
uint8_t
>
(
i
));
for
(
int
i
=
256
;
i
<
513
;
++
i
)
CAF_CHECK_EQUAL
(
rd
(
std
::
to_string
(
i
)),
pec
::
integer_overflow
);
}
CAF_TEST
(
add
ascii
-
signed
)
{
auto
rd
=
[](
string_view
str
)
->
expected
<
int8_t
>
{
int8_t
x
=
0
;
for
(
auto
c
:
str
)
if
(
!
detail
::
parser
::
add_ascii
<
10
>
(
x
,
c
))
return
pec
::
integer_overflow
;
return
x
;
};
for
(
int
i
=
0
;
i
<
128
;
++
i
)
CAF_CHECK_EQUAL
(
rd
(
std
::
to_string
(
i
)),
static_cast
<
int8_t
>
(
i
));
for
(
int
i
=
128
;
i
<
513
;
++
i
)
CAF_CHECK_EQUAL
(
rd
(
std
::
to_string
(
i
)),
pec
::
integer_overflow
);
}
CAF_TEST
(
sub
ascii
)
{
auto
rd
=
[](
string_view
str
)
->
expected
<
int8_t
>
{
int8_t
x
=
0
;
for
(
auto
c
:
str
)
if
(
!
detail
::
parser
::
sub_ascii
<
10
>
(
x
,
c
))
return
pec
::
integer_underflow
;
return
x
;
};
// Using sub_ascii in this way behaves as if we'd prefix the number with a
// minus sign, i.e., "123" will result in -123.
for
(
int
i
=
1
;
i
<
129
;
++
i
)
CAF_CHECK_EQUAL
(
rd
(
std
::
to_string
(
i
)),
static_cast
<
int8_t
>
(
-
i
));
for
(
int
i
=
129
;
i
<
513
;
++
i
)
CAF_CHECK_EQUAL
(
rd
(
std
::
to_string
(
i
)),
pec
::
integer_underflow
);
}
CAF_TEST
(
binary
numbers
)
{
CAF_TEST
(
binary
numbers
)
{
/* TODO: use this implementation when switching to C++14
/* TODO: use this implementation when switching to C++14
CHECK_NUMBER(0b0);
CHECK_NUMBER(0b0);
...
...
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