Patchwork acpi: syntaxcheck: strcpy should be replaced with strncpy (LP: #1205215)

login
register
mail settings
Submitter Colin King
Date July 26, 2013, 9:38 a.m.
Message ID <1374831480-6079-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/262101/
State Accepted
Headers show

Comments

Colin King - July 26, 2013, 9:38 a.m.
From: Colin Ian King <colin.king@canonical.com>

Coverity Scan complaining about a strcpy(). Although the buffer is currently
bug enough, it may not be in the future, so use strncpy() to avoid buffer
overflow.

CID null (#1 of 1): Copy into fixed size buffer (STRING_OVERFLOW)2.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/acpi/syntaxcheck/syntaxcheck.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Alex Hung - July 29, 2013, 8:13 a.m.
On 07/26/2013 05:38 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Coverity Scan complaining about a strcpy(). Although the buffer is currently
> bug enough, it may not be in the future, so use strncpy() to avoid buffer
> overflow.
>
> CID null (#1 of 1): Copy into fixed size buffer (STRING_OVERFLOW)2.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/acpi/syntaxcheck/syntaxcheck.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/src/acpi/syntaxcheck/syntaxcheck.c b/src/acpi/syntaxcheck/syntaxcheck.c
> index 4b00480..55bcc7e 100644
> --- a/src/acpi/syntaxcheck/syntaxcheck.c
> +++ b/src/acpi/syntaxcheck/syntaxcheck.c
> @@ -269,7 +269,8 @@ static const char *syntaxcheck_error_level(uint32_t error_code)
>   		return "Unknown";
>
>   	/* AslErrorLevel strings are end-space padded, so strip off end spaces if any */
> -	strcpy(buf, AslErrorLevel[error_level]);
> +	strncpy(buf, AslErrorLevel[error_level], sizeof(buf));
> +	buf[sizeof(buf) -1] = '\0';
>   	ptr = strchr(buf, ' ');
>   	if (ptr)
>   		*ptr = '\0';
>
Acked-by: Alex Hung <alex.hung@canonical.com>
Keng-Yu Lin - July 29, 2013, 9:03 a.m.
On Mon, Jul 29, 2013 at 4:13 PM, Alex Hung <alex.hung@canonical.com> wrote:
> On 07/26/2013 05:38 PM, Colin King wrote:
>>
>> From: Colin Ian King <colin.king@canonical.com>
>>
>> Coverity Scan complaining about a strcpy(). Although the buffer is
>> currently
>> bug enough, it may not be in the future, so use strncpy() to avoid buffer
>> overflow.
>>
>> CID null (#1 of 1): Copy into fixed size buffer (STRING_OVERFLOW)2.
>>
>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>> ---
>>   src/acpi/syntaxcheck/syntaxcheck.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/src/acpi/syntaxcheck/syntaxcheck.c
>> b/src/acpi/syntaxcheck/syntaxcheck.c
>> index 4b00480..55bcc7e 100644
>> --- a/src/acpi/syntaxcheck/syntaxcheck.c
>> +++ b/src/acpi/syntaxcheck/syntaxcheck.c
>> @@ -269,7 +269,8 @@ static const char *syntaxcheck_error_level(uint32_t
>> error_code)
>>                 return "Unknown";
>>
>>         /* AslErrorLevel strings are end-space padded, so strip off end
>> spaces if any */
>> -       strcpy(buf, AslErrorLevel[error_level]);
>> +       strncpy(buf, AslErrorLevel[error_level], sizeof(buf));
>> +       buf[sizeof(buf) -1] = '\0';
>>         ptr = strchr(buf, ' ');
>>         if (ptr)
>>                 *ptr = '\0';
>>
> Acked-by: Alex Hung <alex.hung@canonical.com>
>
> --
> Cheers,
> Alex Hung
>

Acked-by: Keng-Yu Lin <kengyu@canonical.com>

Patch

diff --git a/src/acpi/syntaxcheck/syntaxcheck.c b/src/acpi/syntaxcheck/syntaxcheck.c
index 4b00480..55bcc7e 100644
--- a/src/acpi/syntaxcheck/syntaxcheck.c
+++ b/src/acpi/syntaxcheck/syntaxcheck.c
@@ -269,7 +269,8 @@  static const char *syntaxcheck_error_level(uint32_t error_code)
 		return "Unknown";
 
 	/* AslErrorLevel strings are end-space padded, so strip off end spaces if any */
-	strcpy(buf, AslErrorLevel[error_level]);
+	strncpy(buf, AslErrorLevel[error_level], sizeof(buf));
+	buf[sizeof(buf) -1] = '\0';
 	ptr = strchr(buf, ' ');
 	if (ptr)
 		*ptr = '\0';