Re: [PATCH 04/15] logmsg_reencode: return const buffer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jun 9, 2014 at 2:10 PM, Jeff King <peff@xxxxxxxx> wrote:
> The return value from logmsg_reencode may be either a newly
> allocated buffer or a pointer to the existing commit->buffer.
> We would not want the caller to accidentally free() or
> modify the latter, so let's mark it as const.  We can cast
> away the constness in logmsg_free, but only once we have
> determined that it is a free-able buffer.
>
> Signed-off-by: Jeff King <peff@xxxxxxxx>
> ---
> index 71e2337..47e5b7a 100644
> --- a/revision.c
> +++ b/revision.c
> @@ -2788,7 +2788,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
>  {
>         int retval;
>         const char *encoding;
> -       char *message;
> +       const char *message;
>         struct strbuf buf = STRBUF_INIT;
>
>         if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
> @@ -2830,12 +2830,18 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
>                 format_display_notes(commit->object.sha1, &buf, encoding, 1);
>         }
>
> -       /* Find either in the original commit message, or in the temporary */
> +       /* Find either in the original commit message, or in the temporary.

Style:

    /*
     * Find either...
     */

> +        * Note that we cast away the constness of "message" here. It is
> +        * const because it may come from the cached commit buffer. That's OK,
> +        * because we know that it is modifiable heap memory, and that while
> +        * grep_buffer may modify it for speed, it will restore any
> +        * changes before returning.
> +        */
>         if (buf.len)
>                 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
>         else
>                 retval = grep_buffer(&opt->grep_filter,
> -                                    message, strlen(message));
> +                                    (char *)message, strlen(message));
>         strbuf_release(&buf);
>         logmsg_free(message, commit);
>         return retval;
> --
> 2.0.0.729.g520999f
>
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]