On 2022/11/19 1:40, Kang-Che Sung wrote:
On Friday, November 18, 2022, Xiaoming Ni <[email protected]
<mailto:[email protected]>> wrote:
> Step 2 of micro-refactoring the set_loop function ()
> Extract subfunction get_next_free_loop() from set_loop()
>
> Also fix miss free(try) when stat(try) and mknod fail
>
> function old new delta
> set_loop 758 734 -24
>
------------------------------------------------------------------------------
> (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-24) Total:
-24 bytes
>
> Fixes: 3448914e8cc5 ("mount,losetup: use /dev/loop-control is it exists")
> Signed-off-by: Xiaoming Ni <[email protected]
<mailto:[email protected]>>
> ---
> libbb/loop.c | 55 ++++++++++++++++++++++++----------------------------
> 1 file changed, 25 insertions(+), 30 deletions(-)
>
> diff --git a/libbb/loop.c b/libbb/loop.c
> index c517ceb13..71fd8c1bc 100644
> --- a/libbb/loop.c
> +++ b/libbb/loop.c
> @@ -96,6 +96,20 @@ int FAST_FUNC get_free_loop(void)
> return loopdevno; /* can be -1 if error */
> }
>
> +static int get_next_free_loop(char *dev, int id)
> +{
> + int i = get_free_loop();
> + if (i >= 0) {
> + sprintf(dev, LOOP_FORMAT, i);
> + return 1; /* use /dev/loop-control */
> + } else if (i == -2) {
> + sprintf(dev, LOOP_FORMAT, id);
> + return 2;
> + } else {
> + return -1; /* no free loop devices */
> + }
> +}
I'm a little nervous when the buffer length of `dev` is not passed into
this function. Yes I know the buffer is large enough for the loop device
path that would be printed. But I just wish there would be an assertion
in this function, so that if the function is reused somewhere else, the
developer would know what he is doing.
static int get_next_free_loop(char *dev, size_t dev_size, int id)
{
int loopdevno = get_free_loop();
if (loopdevno >= 0) {
snprintf(dev, dev_size, LOOP_FORMAT, loopdevno);
return 1; /* use /dev/loop-control */
}
if (loopdevno == -2) {
snprintf(dev, dev_size, LOOP_FORMAT, id);
return 2;
}
return -1; /* no free loop devices */
}
If the dev_size parameter is added to get_next_free_loop(), the code
size increases, Is it worth?
function old new delta
set_loop 734 744 +10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 10/0) Total: 10
bytes
Thanks
Xiaoming Ni
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox