]> Devoid-pointer.net GitWeb - KLGD.git/commitdiff
- Allocate struct klgd_command correctly
authorMichal Malý <madcatxster@devoid-pointer.net>
Mon, 25 Aug 2014 23:52:28 +0000 (01:52 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Mon, 25 Aug 2014 23:52:28 +0000 (01:52 +0200)
- Prevent modification of bytes pointer in struct klgd_command

klgd.c
klgd.h

diff --git a/klgd.c b/klgd.c
index e20c1cf4b73824bcad6ed826b4007ac474348272..2050663fdb974969fed82d8bb7934db48b54d8b0 100644 (file)
--- 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 28acc646fb483aecd20773289070a7b865aeb8c3..c2cdb6dd4e4aeb10e1d4247a26e4e986f609e397 100644 (file)
--- a/klgd.h
+++ b/klgd.h
@@ -1,6 +1,6 @@
 
 struct klgd_command {
-       char *bytes;
+       char * const bytes;
        size_t length;
 };