Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
NuttX RTOS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
f4grx
NuttX RTOS
Commits
15c92867
Commit
15c92867
authored
8 years ago
by
Sebastien Lorquet
Browse files
Options
Downloads
Patches
Plain Diff
Fixes for strtoul/strtoull. Fixes Issue #1
parent
5073bf1d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
libc/stdlib/lib_checkbase.c
+11
-0
11 additions, 0 deletions
libc/stdlib/lib_checkbase.c
libc/stdlib/lib_strtoul.c
+25
-5
25 additions, 5 deletions
libc/stdlib/lib_strtoul.c
libc/stdlib/lib_strtoull.c
+25
-3
25 additions, 3 deletions
libc/stdlib/lib_strtoull.c
with
61 additions
and
8 deletions
libc/stdlib/lib_checkbase.c
+
11
−
0
View file @
15c92867
...
...
@@ -63,6 +63,11 @@
* Assumptions:
* *ptr points to the first, non-whitespace character in the string.
*
* Returns:
* - if base is valid, the actual base to use, and pptr is updated to point
* at the first digit.
* - if base is invalid (<2 or >36), return -1.
*
****************************************************************************/
int
lib_checkbase
(
int
base
,
FAR
const
char
**
pptr
)
...
...
@@ -107,6 +112,12 @@ int lib_checkbase(int base, FAR const char **pptr)
}
}
/* Check for incorrect bases. */
else
if
(
base
<
2
||
base
>
26
)
{
return
-
1
;
/* Means incorrect base */
}
/* Return the updated pointer and base */
*
pptr
=
ptr
;
...
...
This diff is collapsed.
Click to expand it.
libc/stdlib/lib_strtoul.c
+
25
−
5
View file @
15c92867
...
...
@@ -59,13 +59,17 @@
* nptr to a long unsigned integer value according to the given base, which
* must be between 2 and 36 inclusive, or be the special value 0.
*
* Warning: does not check for integer overflow!
* Returns:
* - The converted value, if the base and number are valid
* - 0 if an error occurs, and seterrno to:
* * EINVAL if base < 2 or base > 36
* * ERANGE if the number cannot be represented using unsigned long
*
****************************************************************************/
unsigned
long
strtoul
(
FAR
const
char
*
nptr
,
FAR
char
**
endptr
,
int
base
)
{
unsigned
long
accum
=
0
;
unsigned
long
prev
,
accum
=
0
;
int
value
;
if
(
nptr
)
...
...
@@ -74,16 +78,32 @@ unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base)
lib_skipspace
(
&
nptr
);
/* Check for unspecified base */
/* Check for unspecified
or incorrect
base */
base
=
lib_checkbase
(
base
,
&
nptr
);
if
(
base
<
0
)
{
set_errno
(
EINVAL
);
return
0
;
}
/* Accumulate each "digit" */
while
(
lib_isbasedigit
(
*
nptr
,
base
,
&
value
))
{
accum
=
accum
*
base
+
value
;
nptr
++
;
prev
=
accum
;
accum
=
accum
*
base
+
value
;
nptr
++
;
/* Check for overflow */
if
(
accum
<
prev
)
{
set_errno
(
ERANGE
);
accum
=
0
;
break
;
}
}
/* Return the final pointer to the unused value */
...
...
This diff is collapsed.
Click to expand it.
libc/stdlib/lib_strtoull.c
+
25
−
3
View file @
15c92867
...
...
@@ -62,11 +62,17 @@
* nptr to a long unsigned integer value according to the given base, which
* must be between 2 and 36 inclusive, or be the special value 0.
*
* Returns:
* - The converted value, if the base and number are valid
* - 0 if an error occurs, and seterrno to:
* * EINVAL if base < 2 or base > 36
* * ERANGE if the number cannot be represented using unsigned long long
*
****************************************************************************/
unsigned
long
long
strtoull
(
FAR
const
char
*
nptr
,
FAR
char
**
endptr
,
int
base
)
{
unsigned
long
long
accum
=
0
;
unsigned
long
long
prev
,
accum
=
0
;
int
value
;
if
(
nptr
)
...
...
@@ -79,12 +85,28 @@ unsigned long long strtoull(FAR const char *nptr, FAR char **endptr, int base)
base
=
lib_checkbase
(
base
,
&
nptr
);
if
(
base
<
0
)
{
set_errno
(
EINVAL
);
return
0
;
}
/* Accumulate each "digit" */
while
(
lib_isbasedigit
(
*
nptr
,
base
,
&
value
))
{
accum
=
accum
*
base
+
value
;
nptr
++
;
prev
=
accum
;
accum
=
accum
*
base
+
value
;
nptr
++
;
/* Check for overflow */
if
(
accum
<
prev
)
{
set_errno
(
ERANGE
);
accum
=
0
;
break
;
}
}
/* Return the final pointer to the unused value */
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment