From: Michal MalĂ˝ Date: Mon, 25 Aug 2014 23:52:28 +0000 (+0200) Subject: - Allocate struct klgd_command correctly X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=8826fbb528d2517f250d125c88598b14dca316f5;p=KLGD.git - Allocate struct klgd_command correctly - Prevent modification of bytes pointer in struct klgd_command --- diff --git a/klgd.c b/klgd.c index e20c1cf..2050663 100644 --- a/klgd.c +++ b/klgd.c @@ -31,9 +31,18 @@ static void klgd_schedule_update(struct klgd_main_private *priv); struct klgd_command * klgd_alloc_cmd(const size_t length) { - struct klgd_command *cmd = kzalloc(sizeof(struct klgd_command) * length, GFP_KERNEL); + struct klgd_command *cmd = kzalloc(sizeof(struct klgd_command), GFP_KERNEL); + char *bytes; if (!cmd) return NULL; + + /* Cast away the const-ness */ + bytes = kzalloc(sizeof(char) * length, GFP_KERNEL); + if (!bytes) { + kfree(cmd); + return NULL; + } + *(char **)(&cmd->bytes) = bytes; cmd->length = length; return cmd; } @@ -178,6 +187,14 @@ out: mutex_unlock(&priv->plugins_lock); } +static void klgd_free_command(const struct klgd_command *cmd) +{ + if (cmd) { + kfree(cmd->bytes); + kfree(cmd); + } +} + static void klgd_free_stream(struct klgd_command_stream *s) { size_t idx; @@ -185,10 +202,8 @@ static void klgd_free_stream(struct klgd_command_stream *s) if (!s) return; - for (idx = 0; idx < s->count; idx++) { - kfree(s->commands[idx]->bytes); - kfree(s->commands[idx]); - } + for (idx = 0; idx < s->count; idx++) + klgd_free_command(s->commands[idx]); } void klgd_deinit(struct klgd_main *ctx) diff --git a/klgd.h b/klgd.h index 28acc64..c2cdb6d 100644 --- a/klgd.h +++ b/klgd.h @@ -1,6 +1,6 @@ struct klgd_command { - char *bytes; + char * const bytes; size_t length; };