Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
c9036f3a
Commit
c9036f3a
authored
Feb 05, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added workaround for non-compliant runtimes in alloc_vprintf()
parent
60858d7f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
13 deletions
+49
-13
mongoose.c
mongoose.c
+24
-13
unit_test.c
unit_test.c
+25
-0
No files found.
mongoose.c
View file @
c9036f3a
...
@@ -731,21 +731,32 @@ static int alloc_vprintf(char **buf, size_t size, const char *fmt, va_list ap) {
...
@@ -731,21 +731,32 @@ static int alloc_vprintf(char **buf, size_t size, const char *fmt, va_list ap) {
va_list
ap_copy
;
va_list
ap_copy
;
int
len
;
int
len
;
// Windows is not standard-compliant, and vsnprintf() returns -1 if
// buffer is too small. Also, older versions of msvcrt.dll do not have
// _vscprintf(). However, if size is 0, vsnprintf() behaves correctly.
// Therefore, we make two passes: on first pass, get required message length.
// On second pass, actually print the message.
va_copy
(
ap_copy
,
ap
);
va_copy
(
ap_copy
,
ap
);
len
=
vsnprintf
(
NULL
,
0
,
fmt
,
ap_copy
);
len
=
vsnprintf
(
*
buf
,
size
,
fmt
,
ap_copy
);
va_end
(
ap_copy
);
if
(
len
>
(
int
)
size
&&
if
(
len
<
0
)
{
(
size
=
len
+
1
)
>
0
&&
// eCos and Windows are not standard-compliant and return -1 when
(
*
buf
=
(
char
*
)
malloc
(
size
))
==
NULL
)
{
// the buffer is too small. Keep allocating larger buffers until we
len
=
-
1
;
// Allocation failed, mark failure
// succeed or out of memory.
}
else
{
*
buf
=
NULL
;
va_copy
(
ap_copy
,
ap
);
while
(
len
<
0
)
{
vsnprintf
(
*
buf
,
size
,
fmt
,
ap_copy
);
if
(
*
buf
)
free
(
*
buf
);
size
*=
2
;
if
((
*
buf
=
(
char
*
)
malloc
(
size
))
==
NULL
)
break
;
va_copy
(
ap_copy
,
ap
);
len
=
vsnprintf
(
*
buf
,
size
,
fmt
,
ap_copy
);
va_end
(
ap_copy
);
}
}
else
if
(
len
>
(
int
)
size
)
{
// Standard-compliant code path. Allocate a buffer that is large enough.
if
((
*
buf
=
(
char
*
)
malloc
(
len
+
1
))
==
NULL
)
{
len
=
-
1
;
}
else
{
va_copy
(
ap_copy
,
ap
);
len
=
vsnprintf
(
*
buf
,
len
+
1
,
fmt
,
ap_copy
);
va_end
(
ap_copy
);
}
}
}
return
len
;
return
len
;
...
...
unit_test.c
View file @
c9036f3a
...
@@ -613,6 +613,30 @@ static const char *test_rewrites(void) {
...
@@ -613,6 +613,30 @@ static const char *test_rewrites(void) {
return
NULL
;
return
NULL
;
}
}
static
int
avt
(
char
**
buf
,
size_t
buf_size
,
const
char
*
fmt
,
...)
{
int
result
;
va_list
ap
;
va_start
(
ap
,
fmt
);
result
=
alloc_vprintf
(
buf
,
buf_size
,
fmt
,
ap
);
va_end
(
ap
);
return
result
;
}
static
const
char
*
test_alloc_vprintf
(
void
)
{
char
buf
[
5
],
*
p
=
buf
;
ASSERT
(
avt
(
&
p
,
sizeof
(
buf
),
"%d"
,
123
)
==
3
);
ASSERT
(
p
==
buf
);
ASSERT
(
strcmp
(
p
,
"123"
)
==
0
);
ASSERT
(
avt
(
&
p
,
sizeof
(
buf
),
"%d"
,
123456789
)
==
9
);
ASSERT
(
p
!=
buf
);
ASSERT
(
strcmp
(
p
,
"123456789"
)
==
0
);
free
(
p
);
return
NULL
;
}
static
const
char
*
run_all_tests
(
void
)
{
static
const
char
*
run_all_tests
(
void
)
{
RUN_TEST
(
test_should_keep_alive
);
RUN_TEST
(
test_should_keep_alive
);
RUN_TEST
(
test_match_prefix
);
RUN_TEST
(
test_match_prefix
);
...
@@ -630,6 +654,7 @@ static const char *run_all_tests(void) {
...
@@ -630,6 +654,7 @@ static const char *run_all_tests(void) {
RUN_TEST
(
test_mg_connect
);
RUN_TEST
(
test_mg_connect
);
RUN_TEST
(
test_mg_set_option
);
RUN_TEST
(
test_mg_set_option
);
RUN_TEST
(
test_rewrites
);
RUN_TEST
(
test_rewrites
);
RUN_TEST
(
test_alloc_vprintf
);
#ifdef MONGOOSE_USE_SSL
#ifdef MONGOOSE_USE_SSL
RUN_TEST
(
test_ssl
);
RUN_TEST
(
test_ssl
);
#endif
#endif
...
...
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