On 15.03.2022 17:41, Markus Armbruster wrote:
g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer,
for two reasons. One, it catches multiplication overflowing size_t.
Two, it returns T * rather than void *, which lets the compiler catch
more type errors.
This commit only touches allocations with size arguments of the form
sizeof(T).
Patch created mechanically with:
$ spatch --in-place --sp-file scripts/coccinelle/use-g_new-etc.cocci \
--macro-file scripts/cocci-macro-file.h FILES...
Signed-off-by: Markus Armbruster <armbru@xxxxxxxxxx>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@xxxxxxxxx>
Reviewed-by: Cédric Le Goater <clg@xxxxxxxx>
Reviewed-by: Alex Bennée <alex.bennee@xxxxxxxxxx>
Acked-by: Dr. David Alan Gilbert <dgilbert@xxxxxxxxxx>
---
replay/replay-char.c | 4 +--
replay/replay-events.c | 10 +++---
Reviewed-by: Pavel Dovgalyuk <Pavel.Dovgalyuk@xxxxxxxxx>
diff --git a/replay/replay-char.c b/replay/replay-char.c
index dc0002367e..d2025948cf 100644
--- a/replay/replay-char.c
+++ b/replay/replay-char.c
@@ -50,7 +50,7 @@ void replay_register_char_driver(Chardev *chr)
void replay_chr_be_write(Chardev *s, uint8_t *buf, int len)
{
- CharEvent *event = g_malloc0(sizeof(CharEvent));
+ CharEvent *event = g_new0(CharEvent, 1);
event->id = find_char_driver(s);
if (event->id < 0) {
@@ -85,7 +85,7 @@ void replay_event_char_read_save(void *opaque)
void *replay_event_char_read_load(void)
{
- CharEvent *event = g_malloc0(sizeof(CharEvent));
+ CharEvent *event = g_new0(CharEvent, 1);
event->id = replay_get_byte();
replay_get_array_alloc(&event->buf, &event->len);
diff --git a/replay/replay-events.c b/replay/replay-events.c
index 15983dd250..ac47c89834 100644
--- a/replay/replay-events.c
+++ b/replay/replay-events.c
@@ -119,7 +119,7 @@ void replay_add_event(ReplayAsyncEventKind event_kind,
return;
}
- Event *event = g_malloc0(sizeof(Event));
+ Event *event = g_new0(Event, 1);
event->event_kind = event_kind;
event->opaque = opaque;
event->opaque2 = opaque2;
@@ -243,17 +243,17 @@ static Event *replay_read_event(int checkpoint)
}
break;
case REPLAY_ASYNC_EVENT_INPUT:
- event = g_malloc0(sizeof(Event));
+ event = g_new0(Event, 1);
event->event_kind = replay_state.read_event_kind;
event->opaque = replay_read_input_event();
return event;
case REPLAY_ASYNC_EVENT_INPUT_SYNC:
- event = g_malloc0(sizeof(Event));
+ event = g_new0(Event, 1);
event->event_kind = replay_state.read_event_kind;
event->opaque = 0;
return event;
case REPLAY_ASYNC_EVENT_CHAR_READ:
- event = g_malloc0(sizeof(Event));
+ event = g_new0(Event, 1);
event->event_kind = replay_state.read_event_kind;
event->opaque = replay_event_char_read_load();
return event;
@@ -263,7 +263,7 @@ static Event *replay_read_event(int checkpoint)
}
break;
case REPLAY_ASYNC_EVENT_NET:
- event = g_malloc0(sizeof(Event));
+ event = g_new0(Event, 1);
event->event_kind = replay_state.read_event_kind;
event->opaque = replay_event_net_load();
return event;