Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
magic-lantern
magic-lantern
Commits
6d803ed410fa
Commit
9ffa5d45
authored
Apr 12, 2010
by
hudson@kremvax.wan
Browse files
fix realloc() to avoid double free of NULL
parent
edad54c1abc4
Changes
1
Show whitespace changes
Inline
Side-by-side
stdio.c
View file @
6d803ed4
...
...
@@ -230,28 +230,57 @@ realloc(
size_t
new_size
)
{
struct
dryos_meminfo
*
mem
=
buf
;
// If buf is NULL, this is a normal malloc()
if
(
!
buf
)
{
void
*
rc
=
malloc
(
new_size
);
struct
dryos_meminfo
*
mem
=
rc
;
mem
--
;
DebugMsg
(
DM_MAGIC
,
3
,
"realloc(%08x,%d) = %08x (%08x,%08x,%d)"
,
(
uintptr_t
)
buf
,
new_size
,
(
uintptr_t
)
rc
,
mem
->
next
,
mem
->
prev
,
mem
->
size
);
return
rc
;
}
// If bit 1, 2 or 3 in mem->size is set then it is not valid
if
(
mem
->
size
&
3
)
return
NULL
;
// If new_size is zero, then this is a free
// If new_size is zero, then this is a normal free()
if
(
new_size
==
0
)
{
if
(
buf
)
free
(
buf
);
return
NULL
;
}
struct
dryos_meminfo
*
mem
=
buf
;
mem
--
;
// If bit 1, 2 or 3 in mem->size is set then it is not valid
if
(
mem
->
size
&
3
)
return
NULL
;
// If the new size is less than the current size do nothing
if
(
new_size
<
mem
->
size
)
return
buf
;
// Allocate a new buffer and copy the old data into it
void
*
new_buf
=
malloc
(
new_size
);
memcpy
(
new_buf
,
buf
,
mem
->
size
);
free
(
buf
);
if
(
!
new_buf
)
return
NULL
;
int
i
;
for
(
i
=
0
;
i
<
mem
->
size
/
4
;
i
++
)
{
((
uint32_t
*
)
new_buf
)[
i
]
=
((
uint32_t
*
)
buf
)[
i
];
asm
(
"nop; nop; nop; nop;"
);
}
//free( buf );
// Return a pointer to the new buffer
return
new_buf
;
...
...
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