linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: Sidong Yang <realwakka@gmail.com>,
	Johannes Thumshirn <Johannes.Thumshirn@wdc.com>
Cc: linux-btrfs <linux-btrfs@vger.kernel.org>
Subject: Re: [PATCH v2] btrfs-progs: cmds: Add subcommand that dumps file extents
Date: Wed, 14 Jul 2021 06:16:08 +0800	[thread overview]
Message-ID: <2c0484ff-670d-5a26-0a96-e396289a784f@gmx.com> (raw)
In-Reply-To: <20210713165444.GB71156@realwakka>



On 2021/7/14 上午12:54, Sidong Yang wrote:
> On Mon, Jul 12, 2021 at 06:52:28AM +0000, Johannes Thumshirn wrote:
>
> Hi, Thanks for review!
>
>> On 11/07/2021 18:10, Sidong Yang wrote:
>>> +static void compress_type_to_str(u8 compress_type, char *ret)
>>> +{
>>> +	switch (compress_type) {
>>> +	case BTRFS_COMPRESS_NONE:
>>> +		strcpy(ret, "none");
>>> +		break;
>>> +	case BTRFS_COMPRESS_ZLIB:
>>> +		strcpy(ret, "zlib");
>>> +		break;
>>> +	case BTRFS_COMPRESS_LZO:
>>> +		strcpy(ret, "lzo");
>>> +		break;
>>> +	case BTRFS_COMPRESS_ZSTD:
>>> +		strcpy(ret, "zstd");
>>> +		break;
>>> +	default:
>>> +		sprintf(ret, "UNKNOWN.%d", compress_type);
>>> +	}
>>> +}
>>
>> [....]
>>> +	char compress_str[16];
>>
>> [...]
>>
>>> +					compress_type_to_str(extent_item->compression, compress_str);
>>
>> While this looks safe at a first glance, can we change this to:
>>
>> #define COMPRESS_STR_LEN 5
>>
>> [...]
>>
>> switch (compress_type) {
>> case BTRFS_COMPRESS_NONE:
>> 	strncpy(ret, "none", COMPRESS_STR_LEN);
>>
>> [...]
>>
>> char compress_str[COMPRESS_STR_LEN];
>>
>> One day someone will factor out 'compress_type_to_str()' and make it public, read user
>> supplied input and then it's a disaster waiting to happen.
>
> This can be happen when @ret is too short? If it so, I think it also has
> problem when @ret is shorter than COMPRESS_STR_LEN. I wonder that I
> understood this problem you said.

I guess Johannes means that, if we support more and more new compression
algos, it can go beyond your original 16 bytes length for the
compression algo name.

While using strncpy, it will never go beyond COMPRESS_STR_LEN forever.
(Although it will truncating the output, but that would be easier to
spot the problem and then enlarge the macro)

Thanks,
Qu

>
> Thanks,
> Sidong
>
>>
>> Thanks,
>> 	Johannes

  reply	other threads:[~2021-07-13 22:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-11 16:10 [PATCH v2] btrfs-progs: cmds: Add subcommand that dumps file extents Sidong Yang
2021-07-12  1:16 ` Qu Wenruo
2021-07-12  6:40   ` Sidong Yang
2021-07-12  6:46     ` Qu Wenruo
2021-07-13 16:45       ` Sidong Yang
2021-07-12  6:52 ` Johannes Thumshirn
2021-07-13 16:54   ` Sidong Yang
2021-07-13 22:16     ` Qu Wenruo [this message]
2021-07-14  6:41       ` Sidong Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2c0484ff-670d-5a26-0a96-e396289a784f@gmx.com \
    --to=quwenruo.btrfs@gmx.com \
    --cc=Johannes.Thumshirn@wdc.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=realwakka@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).