Index | Thread | Search

From:
Florian Obser <florian@openbsd.org>
Subject:
Re: file: use better imsg API
To:
tech@openbsd.org
Date:
Wed, 21 Aug 2024 19:17:27 +0200

Download raw body.

Thread
Hang on, there is an imsg_get_data?! Why do I only learn about this now?
I see a flurry of commits in my future...

Anyway, this reads OK florian
On 2024-08-21 17:44 +02, Claudio Jeker <cjeker@diehard.n-r-g.com> wrote:
> Tired of old crufty imsg fiddling. Lets use the real deal.
>
> -- 
> :wq Claudio
>
> Index: file.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/file/file.c,v
> diff -u -p -r1.70 file.c
> --- file.c	16 Jan 2024 13:07:29 -0000	1.70
> +++ file.c	21 Aug 2024 15:41:10 -0000
> @@ -125,7 +125,7 @@ main(int argc, char **argv)
>  	struct imsgbuf		 ibuf;
>  	struct imsg		 imsg;
>  	struct input_msg	 msg;
> -	struct input_ack	*ack;
> +	struct input_ack	 ack;
>  	pid_t			 pid, parent;
>  
>  	tzset();
> @@ -223,10 +223,9 @@ main(int argc, char **argv)
>  
>  		if (read_message(&ibuf, &imsg, pid) == 0)
>  			break;
> -		if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof *ack)
> -			errx(1, "message too small");
> -		ack = imsg.data;
> -		if (ack->idx != idx)
> +		if (imsg_get_data(&imsg, &ack, sizeof ack) == -1)
> +			err(1, "bad message");
> +		if (ack.idx != idx)
>  			errx(1, "index not expected");
>  		imsg_free(&imsg);
>  	}
> @@ -365,7 +364,7 @@ child(int fd, pid_t parent, int argc, ch
>  	struct magic		*m;
>  	struct imsgbuf		 ibuf;
>  	struct imsg		 imsg;
> -	struct input_msg	*msg;
> +	struct input_msg	 msg;
>  	struct input_ack	 ack;
>  	struct input_file	 inf;
>  	int			 i, idx;
> @@ -405,17 +404,16 @@ child(int fd, pid_t parent, int argc, ch
>  	for (;;) {
>  		if (read_message(&ibuf, &imsg, parent) == 0)
>  			break;
> -		if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof *msg)
> -			errx(1, "message too small");
> -		msg = imsg.data;
> +		if (imsg_get_data(&imsg, &msg, sizeof msg) == -1)
> +			err(1, "bad message");
>  
> -		idx = msg->idx;
> +		idx = msg.idx;
>  		if (idx < 0 || idx >= argc)
>  			errx(1, "index out of range");
>  
>  		memset(&inf, 0, sizeof inf);
>  		inf.m = m;
> -		inf.msg = msg;
> +		inf.msg = &msg;
>  
>  		inf.path = argv[idx];
>  		inf.fd = imsg_get_fd(&imsg);
>

-- 
In my defence, I have been left unsupervised.