"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
Re: got: rm * removes current directory
To:
Omar Polo <op@omarpolo.com>
Cc:
Mark Jamsek <mark@jamsek.com>, Mikhail <mp39590@gmail.com>, gameoftrees@openbsd.org
Date:
Tue, 30 May 2023 10:58:10 +0200

Download raw body.

Thread
On Tue, May 30, 2023 at 10:40:24AM +0200, Omar Polo wrote:
> On 2023/05/30 17:45:50 +1000, Mark Jamsek <mark@jamsek.com> wrote:
> > On 23-05-29 06:27PM, Stefan Sperling wrote:
> > > On Mon, May 29, 2023 at 04:56:25PM +0200, Stefan Sperling wrote:
> > > > Like this?
> > > 
> > > Omar pointed out off-list that in this diff I made the mistake of
> > > assigning -1 to a size_t (which is unsigned). So I would prefer to
> > > keep this code as-is to avoid falling into such traps.
> > 
> > Yes, I shouldn't have been so vague in my suggestion and shared a diff
> > instead. I was thinking of using the `if (len == (size_t)-1)` idiom like
> > below to check for asprintf() failure. But now I'm not so sure if
> > keeping its return value just to drop the strlen() calls are
> > a worthwhile optimisation. More often than not 'got rm' takes few
> > arguments so in the rare cases someone is dropping a large directory,
> > walking the string length for each path is fine. And from a cursory
> > look, I don't see this patten used elsewhere in got so for the sake of
> > consistency, it's probably better to keep it as-is.
> 
> just for the sake of the discussion; I'm not a C-lawyer but I think
> just using an `int' (which is what asprintf returns anyway) is cleaner
> and more readable: we're guaranteed it returns the number of bytes or
> -1 if an error occurs, and it fits inside a size_t.

Yes, this would be the right approach. Ok by me if you want to commit it.
At least we'll have a correct example of this in the tree in case
someone goes on a quest to eliminate strlen calls after asprintf.

> diff /home/op/w/got
> commit - fb307946174c95e32d2048584c6ab1ce24f3ea00
> path + /home/op/w/got
> blob - 9d45bcc89fba03349491b41c260b4fc0c9606531
> file + lib/worktree.c
> --- lib/worktree.c
> +++ lib/worktree.c
> @@ -4477,15 +4477,17 @@ got_worktree_schedule_delete(struct got_worktree *work
>  
>  	TAILQ_FOREACH(pe, paths, entry) {
>  		char *ondisk_status_path;
> +		int len;
>  
> -		if (asprintf(&ondisk_status_path, "%s%s%s",
> +		len = asprintf(&ondisk_status_path, "%s%s%s",
>  		    got_worktree_get_root_path(worktree),
> -		    pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
> +		    pe->path[0] == '\0' ? "" : "/", pe->path);
> +		if (len == -1) {
>  			err = got_error_from_errno("asprintf");
>  			goto done;
>  		}
>  		sda.status_path = ondisk_status_path;
> -		sda.status_path_len = strlen(ondisk_status_path);
> +		sda.status_path_len = len;
>  		err = worktree_status(worktree, pe->path, fileindex, repo,
>  			schedule_for_deletion, &sda, NULL, NULL, 1, 1);
>  		free(ondisk_status_path);