git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/4] Convert resolve_ref+xstrdup to new resolve_refdup function

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Dec 13, 2011, 14:09 UTC
Message-ID
<4EE75C88.5060700@alum.mit.edu>
In-Reply-To
<7vpqft4qap.fsf@alter.siamese.dyndns.org>
On 12/12/2011 07:07 PM, Junio C Hamano wrote:
Show 37 quoted lines
> Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
> 
>> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> 
> Thanks.
> 
> The patch looks correct but I have a slight maintainability concern and a
> suggestion for possible improvement.
> 
>> ...
>> diff --git a/builtin/checkout.c b/builtin/checkout.c
>> index b7c6302..a66d3eb 100644
>> --- a/builtin/checkout.c
>> +++ b/builtin/checkout.c
>> @@ -696,17 +696,14 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
>>  {
>>  	int ret = 0;
>>  	struct branch_info old;
>> +	char *path;
>>  	unsigned char rev[20];
>>  	int flag;
>>  	memset(&old, 0, sizeof(old));
>> -	old.path = resolve_ref("HEAD", rev, 0, &flag);
>> -	if (old.path)
>> -		old.path = xstrdup(old.path);
>> +	old.path = path = resolve_refdup("HEAD", rev, 0, &flag);
> 
> This uses "one 'const char *' pointer that is used for reading data from
> and an extra 'char *' pointer that is used only for freeing" approach,
> which has two advantages and one disadvantage:
> [...]
> When naming a "for-freeing" pointer variable, the kind of data the area of
> memory happens to contain is of secondary importance. Being deliberately
> vague about what the area of memory may contain is a good thing, because
> it actively discourages the program from looking at the area via the
> pointer if the variable is named "to_free" or something that does not
> specify what it contains.

The to_free variable could even be declared void* to make it even less (ab)usable.

Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 5 of 9 in “revert: convert resolve_ref() to read_ref_full()”
  1. 1/4 revert: convert resolve_ref() to read_ref_full()Nguyễn Thái Ngọc Duy, Dec 12, 2011
  2. 2/4 Convert resolve_ref+xstrdup to new resolve_refdup functionNguyễn Thái Ngọc Duy, Dec 12, 2011
  3. Junio C HamanoDec 12, 2011
  4. Convert resolve_ref+xstrdup to new resolve_refdup functionNguyễn Thái Ngọc Duy, Dec 13, 2011
  5. Michael HaggertyDec 13, 2011
  6. Convert resolve_ref+xstrdup to new resolve_refdup functionNguyễn Thái Ngọc Duy, Dec 13, 2011
  7. 3/4 Guard memory overwriting in resolve_ref's static bufferNguyễn Thái Ngọc Duy, Dec 12, 2011
  8. Junio C HamanoDec 13, 2011
  9. 4/4 Rename resolve_ref() to resolve_ref_unsafe()Nguyễn Thái Ngọc Duy, Dec 12, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.