Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
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
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
Libraries
mruby
Commits
dd603d49
Unverified
Commit
dd603d49
authored
4 years ago
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
4 years ago
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5306 from dearblue/unpack
Fixed `String#unpack` to handle the highest range of integer values
parents
82524120
b500e829
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
33 deletions
+13
-33
mrbgems/mruby-pack/src/pack.c
mrbgems/mruby-pack/src/pack.c
+13
-33
No files found.
mrbgems/mruby-pack/src/pack.c
View file @
dd603d49
...
...
@@ -15,6 +15,9 @@
#include <errno.h>
#include <string.h>
#define INT_OVERFLOW_P(n) ((n) < MRB_INT_MIN || (n) > MRB_INT_MAX)
#define UINT_OVERFLOW_P(n) ((n) > MRB_INT_MAX)
struct
tmpl
{
mrb_value
str
;
int
idx
;
...
...
@@ -213,26 +216,6 @@ u32tostr(char *buf, size_t len, uint32_t n)
snprintf
(
buf
,
len
,
"%"
PRIu32
,
n
);
#endif
/* MRB_NO_STDIO */
}
static
void
i32tostr
(
char
*
buf
,
size_t
len
,
int32_t
n
)
{
#ifdef MRB_NO_STDIO
if
(
len
<
1
)
{
return
;
}
if
(
n
<
0
)
{
*
buf
++
=
'-'
;
len
--
;
n
=
-
n
;
}
u32tostr
(
buf
,
len
,
(
uint32_t
)
n
);
#else
snprintf
(
buf
,
len
,
"%"
PRId32
,
n
);
#endif
/* MRB_NO_STDIO */
}
#endif
/* MRB_INT64 */
static
int
...
...
@@ -256,24 +239,17 @@ unpack_l(mrb_state *mrb, const unsigned char *src, int srclen, mrb_value ary, un
ul
+=
(
uint32_t
)
src
[
3
];
}
if
(
flags
&
PACK_FLAG_SIGNED
)
{
int32_t
sl
=
ul
;
#ifndef MRB_INT64
if
(
!
FIXABLE
(
sl
))
{
i32tostr
(
msg
,
sizeof
(
msg
),
sl
);
mrb_raisef
(
mrb
,
E_RANGE_ERROR
,
"cannot unpack to Integer: %s"
,
msg
);
}
#endif
n
=
sl
;
n
=
(
int32_t
)
ul
;
}
else
{
#ifndef MRB_INT64
if
(
!
POSFIXABLE
(
ul
))
{
if
(
UINT_OVERFLOW_P
(
ul
))
{
u32tostr
(
msg
,
sizeof
(
msg
),
ul
);
mrb_raisef
(
mrb
,
E_RANGE_ERROR
,
"cannot unpack to Integer: %s"
,
msg
);
}
#endif
n
=
ul
;
}
mrb_ary_push
(
mrb
,
ary
,
mrb_
fixnum_value
(
n
));
mrb_ary_push
(
mrb
,
ary
,
mrb_
int_value
(
mrb
,
n
));
return
4
;
}
...
...
@@ -337,6 +313,7 @@ u64tostr(char *buf, size_t len, uint64_t n)
#endif
/* MRB_NO_STDIO */
}
#ifndef MRB_INT64
static
void
i64tostr
(
char
*
buf
,
size_t
len
,
int64_t
n
)
{
...
...
@@ -356,6 +333,7 @@ i64tostr(char *buf, size_t len, int64_t n)
snprintf
(
buf
,
len
,
"%"
PRId64
,
n
);
#endif
/* MRB_NO_STDIO */
}
#endif
/* MRB_INT64 */
static
int
unpack_q
(
mrb_state
*
mrb
,
const
unsigned
char
*
src
,
int
srclen
,
mrb_value
ary
,
unsigned
int
flags
)
...
...
@@ -379,19 +357,21 @@ unpack_q(mrb_state *mrb, const unsigned char *src, int srclen, mrb_value ary, un
}
if
(
flags
&
PACK_FLAG_SIGNED
)
{
int64_t
sll
=
ull
;
if
(
!
FIXABLE
(
sll
))
{
#ifndef MRB_INT64
if
(
INT_OVERFLOW_P
(
sll
))
{
i64tostr
(
msg
,
sizeof
(
msg
),
sll
);
mrb_raisef
(
mrb
,
E_RANGE_ERROR
,
"cannot unpack to Integer: %s"
,
msg
);
}
#endif
n
=
(
mrb_int
)
sll
;
}
else
{
if
(
!
POSFIXABLE
(
ull
))
{
if
(
UINT_OVERFLOW_P
(
ull
))
{
u64tostr
(
msg
,
sizeof
(
msg
),
ull
);
mrb_raisef
(
mrb
,
E_RANGE_ERROR
,
"cannot unpack to Integer: %s"
,
msg
);
}
n
=
(
mrb_int
)
ull
;
}
mrb_ary_push
(
mrb
,
ary
,
mrb_
fixnum_value
(
n
));
mrb_ary_push
(
mrb
,
ary
,
mrb_
int_value
(
mrb
,
n
));
return
8
;
}
...
...
This diff is collapsed.
Click to expand it.
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