From: Michael Ellerman <mpe@ellerman.id.au>
To: Daniel Axtens <dja@axtens.net>, linuxppc-dev@lists.ozlabs.org
Subject: Re: [PATCH 1/2] powerpc/prom_init: Convert prom_strcpy() into prom_strscpy_pad()
Date: Tue, 22 Jun 2021 14:11:39 +1000 [thread overview]
Message-ID: <87bl7y35dw.fsf@mpe.ellerman.id.au> (raw)
In-Reply-To: <87lf73iddy.fsf@dja-thinkpad.axtens.net>
Daniel Axtens <dja@axtens.net> writes:
> Hi
>
>> -static char __init *prom_strcpy(char *dest, const char *src)
>> +static ssize_t __init prom_strscpy_pad(char *dest, const char *src, size_t n)
>> {
>> - char *tmp = dest;
>> + ssize_t rc;
>> + size_t i;
>>
>> - while ((*dest++ = *src++) != '\0')
>> - /* nothing */;
>> - return tmp;
>> + if (n == 0 || n > INT_MAX)
>> + return -E2BIG;
>> +
>> + // Copy up to n bytes
>> + for (i = 0; i < n && src[i] != '\0'; i++)
>> + dest[i] = src[i];
>> +
>> + rc = i;
>> +
>> + // If we copied all n then we have run out of space for the nul
>> + if (rc == n) {
>> + // Rewind by one character to ensure nul termination
>> + i--;
>> + rc = -E2BIG;
>> + }
>> +
>> + for (; i < n; i++)
>> + dest[i] = '\0';
>> +
>> + return rc;
>> }
>>
>
> This implementation seems good to me.
>
> I copied it into a new C file and added the following:
>
> int main() {
> char longstr[255]="abcdefghijklmnopqrstuvwxyz";
> char shortstr[5];
> assert(prom_strscpy_pad(longstr, "", 0) == -E2BIG);
> assert(prom_strscpy_pad(longstr, "hello", 255) == 5);
> assert(prom_strscpy_pad(shortstr, "hello", 5) == -E2BIG);
> assert(memcmp(shortstr, "hell", 5) == 0);
> assert(memcmp(longstr, "hello\0\0\0\0\0\0\0\0\0", 6) == 0);
> return 0;
> }
>
> All the assertions pass. I believe this covers all the conditions from
> the strscpy_pad docstring.
>
> Reviewed-by: Daniel Axtens <dja@axtens.net>
Thanks.
I'll also drop the explicit nul termination in patch 2, which is a
leftover from when I was using strncpy().
cheers
next prev parent reply other threads:[~2021-06-22 4:12 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-21 6:49 [PATCH 1/2] powerpc/prom_init: Convert prom_strcpy() into prom_strscpy_pad() Michael Ellerman
2021-06-21 6:49 ` [PATCH 2/2] powerpc/prom_init: Pass linux_banner to firmware via option vector 7 Michael Ellerman
2021-06-22 18:11 ` Tyrel Datwyler
2021-06-23 0:38 ` Michael Ellerman
2021-06-21 12:57 ` [PATCH 1/2] powerpc/prom_init: Convert prom_strcpy() into prom_strscpy_pad() Daniel Axtens
2021-06-22 4:11 ` Michael Ellerman [this message]
2021-06-22 18:12 ` Tyrel Datwyler
2021-06-25 6:21 ` Michael Ellerman
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=87bl7y35dw.fsf@mpe.ellerman.id.au \
--to=mpe@ellerman.id.au \
--cc=dja@axtens.net \
--cc=linuxppc-dev@lists.ozlabs.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.