]> git.proxmox.com Git - libgit2.git/commitdiff
describe example: fix memory allocation size
authorBoris Egorov <egorov@linux.com>
Sun, 25 Jan 2015 16:34:46 +0000 (22:34 +0600)
committerBoris Egorov <egorov@linux.com>
Sun, 25 Jan 2015 16:34:46 +0000 (22:34 +0600)
We need to allocate memory for sizeof(char *) * ncommits, not just for
ncommits.

Issue detected by GCC's AddressSanitizer.

examples/describe.c

index 09a4fd008d6ecd44b46322c374db95c91259d2f4..72d9811534357bf26a2afe735868211ec90be8ae 100644 (file)
@@ -96,7 +96,8 @@ static void parse_options(describe_options *opts, int argc, char **argv)
                const char *curr = argv[args.pos];
 
                if (curr[0] != '-') {
-                       opts->commits = (const char **)realloc((void *)opts->commits, ++opts->commit_count);
+                       size_t newsz = ++opts->commit_count * sizeof(opts->commits[0]);
+                       opts->commits = (const char **)realloc((void *)opts->commits, newsz);
                        opts->commits[opts->commit_count - 1] = curr;
                } else if (!strcmp(curr, "--all")) {
                        opts->describe_options.describe_strategy = GIT_DESCRIBE_ALL;
@@ -123,7 +124,8 @@ static void parse_options(describe_options *opts, int argc, char **argv)
        }
        else {
                if (!opts->format_options.dirty_suffix || !opts->format_options.dirty_suffix[0]) {
-                       opts->commits = (const char **)malloc(++opts->commit_count);
+                       size_t sz = ++opts->commit_count * sizeof(opts->commits[0]);
+                       opts->commits = (const char **)malloc(sz);
                        opts->commits[0] = "HEAD";
                }
        }