patches for DPDK stable branches
 help / color / mirror / Atom feed
* [dpdk-stable] [PATCH v1] examples/vm_power_manager: fix string null termination
@ 2019-04-26  8:43 David Hunt
  2019-04-26 10:33 ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
  2019-04-26 11:24 ` [dpdk-stable] [PATCH v2] " David Hunt
  0 siblings, 2 replies; 10+ messages in thread
From: David Hunt @ 2019-04-26  8:43 UTC (permalink / raw)
  To: dev; +Cc: david.hunt, stable

coverity complains about a null-termination after a read,
so we terminate conditionally on whether idx is within
the buffer or at the end of the buffer.

Coverity issue: 337680
Fixes: a63504a90f ("examples/power: add JSON string handling")
CC: stable@dpdk.org
Signed-off-by: David Hunt <david.hunt@intel.com>
---
 examples/vm_power_manager/channel_monitor.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/examples/vm_power_manager/channel_monitor.c b/examples/vm_power_manager/channel_monitor.c
index 971e4f2bc..711722fef 100644
--- a/examples/vm_power_manager/channel_monitor.c
+++ b/examples/vm_power_manager/channel_monitor.c
@@ -808,6 +808,7 @@ read_json_packet(struct channel_info *chan_info)
 		int indent = 0;
 		do {
 			n_bytes = read(chan_info->fd, &json_data[idx], 1);
+			json_data[idx + (idx < MAX_JSON_STRING_LEN - 1)] = '\0';
 			if (n_bytes == 0)
 				break;
 			if (json_data[idx] == '{')
-- 
2.17.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [dpdk-stable] [dpdk-dev] [PATCH v1] examples/vm_power_manager: fix string null termination
  2019-04-26  8:43 [dpdk-stable] [PATCH v1] examples/vm_power_manager: fix string null termination David Hunt
@ 2019-04-26 10:33 ` Burakov, Anatoly
  2019-04-26 11:16   ` Hunt, David
  2019-04-26 11:24 ` [dpdk-stable] [PATCH v2] " David Hunt
  1 sibling, 1 reply; 10+ messages in thread
From: Burakov, Anatoly @ 2019-04-26 10:33 UTC (permalink / raw)
  To: David Hunt, dev; +Cc: stable

On 26-Apr-19 9:43 AM, David Hunt wrote:
> coverity complains about a null-termination after a read,
> so we terminate conditionally on whether idx is within
> the buffer or at the end of the buffer.
> 
> Coverity issue: 337680
> Fixes: a63504a90f ("examples/power: add JSON string handling")
> CC: stable@dpdk.org
> Signed-off-by: David Hunt <david.hunt@intel.com>
> ---
>   examples/vm_power_manager/channel_monitor.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/examples/vm_power_manager/channel_monitor.c b/examples/vm_power_manager/channel_monitor.c
> index 971e4f2bc..711722fef 100644
> --- a/examples/vm_power_manager/channel_monitor.c
> +++ b/examples/vm_power_manager/channel_monitor.c
> @@ -808,6 +808,7 @@ read_json_packet(struct channel_info *chan_info)
>   		int indent = 0;
>   		do {
>   			n_bytes = read(chan_info->fd, &json_data[idx], 1);
> +			json_data[idx + (idx < MAX_JSON_STRING_LEN - 1)] = '\0';

Why do it inside the loop and not after?

>   			if (n_bytes == 0)
>   				break;
>   			if (json_data[idx] == '{')
> 


-- 
Thanks,
Anatoly

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [dpdk-stable] [dpdk-dev] [PATCH v1] examples/vm_power_manager: fix string null termination
  2019-04-26 10:33 ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
@ 2019-04-26 11:16   ` Hunt, David
  0 siblings, 0 replies; 10+ messages in thread
From: Hunt, David @ 2019-04-26 11:16 UTC (permalink / raw)
  To: Burakov, Anatoly, dev; +Cc: stable

Hi Anatoly,

On 26/4/2019 11:33 AM, Burakov, Anatoly wrote:
> On 26-Apr-19 9:43 AM, David Hunt wrote:
>> coverity complains about a null-termination after a read,
>> so we terminate conditionally on whether idx is within
>> the buffer or at the end of the buffer.
>>
>> Coverity issue: 337680
>> Fixes: a63504a90f ("examples/power: add JSON string handling")
>> CC: stable@dpdk.org
>> Signed-off-by: David Hunt <david.hunt@intel.com>
>> ---
>>   examples/vm_power_manager/channel_monitor.c | 1 +
>>   1 file changed, 1 insertion(+)
>>
>> diff --git a/examples/vm_power_manager/channel_monitor.c 
>> b/examples/vm_power_manager/channel_monitor.c
>> index 971e4f2bc..711722fef 100644
>> --- a/examples/vm_power_manager/channel_monitor.c
>> +++ b/examples/vm_power_manager/channel_monitor.c
>> @@ -808,6 +808,7 @@ read_json_packet(struct channel_info *chan_info)
>>           int indent = 0;
>>           do {
>>               n_bytes = read(chan_info->fd, &json_data[idx], 1);
>> +            json_data[idx + (idx < MAX_JSON_STRING_LEN - 1)] = '\0';
>
> Why do it inside the loop and not after?
>

No reason, really. I'll move it outside and re-spin.


>>               if (n_bytes == 0)
>>                   break;
>>               if (json_data[idx] == '{')
>>
>
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [dpdk-stable] [PATCH v2] examples/vm_power_manager: fix string null termination
  2019-04-26  8:43 [dpdk-stable] [PATCH v1] examples/vm_power_manager: fix string null termination David Hunt
  2019-04-26 10:33 ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
@ 2019-04-26 11:24 ` David Hunt
  2019-04-26 11:56   ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
  2019-04-26 14:04   ` [dpdk-stable] [PATCH v3] " David Hunt
  1 sibling, 2 replies; 10+ messages in thread
From: David Hunt @ 2019-04-26 11:24 UTC (permalink / raw)
  To: dev; +Cc: david.hunt, stable

coverity complains about a null-termination after a read,
so we terminate after exiting the do-while loop. The position
is conditional on whether idx is within the buffer or at the
end of the buffer.

Coverity issue: 337680
Fixes: a63504a90f ("examples/power: add JSON string handling")
CC: stable@dpdk.org

Signed-off-by: David Hunt <david.hunt@intel.com>

---
v2:
   * Move null termination outside of do-while.
---
 examples/vm_power_manager/channel_monitor.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/examples/vm_power_manager/channel_monitor.c b/examples/vm_power_manager/channel_monitor.c
index 971e4f2bc..03fdcd15a 100644
--- a/examples/vm_power_manager/channel_monitor.c
+++ b/examples/vm_power_manager/channel_monitor.c
@@ -822,6 +822,8 @@ read_json_packet(struct channel_info *chan_info)
 				break;
 		} while (indent > 0);
 
+		json_data[idx + (idx < MAX_JSON_STRING_LEN - 1)] = '\0';
+
 		if (indent > 0)
 			/*
 			 * We've broken out of the read loop without getting
-- 
2.17.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [dpdk-stable] [dpdk-dev] [PATCH v2] examples/vm_power_manager: fix string null termination
  2019-04-26 11:24 ` [dpdk-stable] [PATCH v2] " David Hunt
@ 2019-04-26 11:56   ` Burakov, Anatoly
  2019-04-26 12:31     ` Bruce Richardson
  2019-04-26 14:04   ` [dpdk-stable] [PATCH v3] " David Hunt
  1 sibling, 1 reply; 10+ messages in thread
From: Burakov, Anatoly @ 2019-04-26 11:56 UTC (permalink / raw)
  To: David Hunt, dev; +Cc: stable

On 26-Apr-19 12:24 PM, David Hunt wrote:
> coverity complains about a null-termination after a read,
> so we terminate after exiting the do-while loop. The position
> is conditional on whether idx is within the buffer or at the
> end of the buffer.
> 
> Coverity issue: 337680
> Fixes: a63504a90f ("examples/power: add JSON string handling")
> CC: stable@dpdk.org
> 
> Signed-off-by: David Hunt <david.hunt@intel.com>
> 
> ---
> v2:
>     * Move null termination outside of do-while.
> ---
>   examples/vm_power_manager/channel_monitor.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/examples/vm_power_manager/channel_monitor.c b/examples/vm_power_manager/channel_monitor.c
> index 971e4f2bc..03fdcd15a 100644
> --- a/examples/vm_power_manager/channel_monitor.c
> +++ b/examples/vm_power_manager/channel_monitor.c
> @@ -822,6 +822,8 @@ read_json_packet(struct channel_info *chan_info)
>   				break;
>   		} while (indent > 0);
>   
> +		json_data[idx + (idx < MAX_JSON_STRING_LEN - 1)] = '\0';
> +

I don't think you need this complicated logic here. You start at idx = 
0, so even if you receive 0 bytes, you'll terminate buffer at index 0. 
You also break when idx reaches (MAX_JSON_STRING_LEN - 1), so it's also 
safe to do json_data[idx] after the loop. In all other cases, you still 
increment idx before breaking out (e.g. when reaching indent == 0), so 
it's also safe to do json_data[idx] in those cases.

>   		if (indent > 0)
>   			/*
>   			 * We've broken out of the read loop without getting
> 


-- 
Thanks,
Anatoly

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [dpdk-stable] [dpdk-dev] [PATCH v2] examples/vm_power_manager: fix string null termination
  2019-04-26 11:56   ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
@ 2019-04-26 12:31     ` Bruce Richardson
  2019-04-26 12:47       ` Burakov, Anatoly
  0 siblings, 1 reply; 10+ messages in thread
From: Bruce Richardson @ 2019-04-26 12:31 UTC (permalink / raw)
  To: Burakov, Anatoly; +Cc: David Hunt, dev, stable

On Fri, Apr 26, 2019 at 12:56:08PM +0100, Burakov, Anatoly wrote:
> On 26-Apr-19 12:24 PM, David Hunt wrote:
> > coverity complains about a null-termination after a read,
> > so we terminate after exiting the do-while loop. The position
> > is conditional on whether idx is within the buffer or at the
> > end of the buffer.
> > 
> > Coverity issue: 337680
> > Fixes: a63504a90f ("examples/power: add JSON string handling")
> > CC: stable@dpdk.org
> > 
> > Signed-off-by: David Hunt <david.hunt@intel.com>
> > 
> > ---
> > v2:
> >     * Move null termination outside of do-while.
> > ---
> >   examples/vm_power_manager/channel_monitor.c | 2 ++
> >   1 file changed, 2 insertions(+)
> > 
> > diff --git a/examples/vm_power_manager/channel_monitor.c b/examples/vm_power_manager/channel_monitor.c
> > index 971e4f2bc..03fdcd15a 100644
> > --- a/examples/vm_power_manager/channel_monitor.c
> > +++ b/examples/vm_power_manager/channel_monitor.c
> > @@ -822,6 +822,8 @@ read_json_packet(struct channel_info *chan_info)
> >   				break;
> >   		} while (indent > 0);
> > +		json_data[idx + (idx < MAX_JSON_STRING_LEN - 1)] = '\0';
> > +
> 
> I don't think you need this complicated logic here. You start at idx = 0, so
> even if you receive 0 bytes, you'll terminate buffer at index 0. You also
> break when idx reaches (MAX_JSON_STRING_LEN - 1), so it's also safe to do
> json_data[idx] after the loop. In all other cases, you still increment idx
> before breaking out (e.g. when reaching indent == 0), so it's also safe to
> do json_data[idx] in those cases.
> 
+1 to that.

An alternative and simpler option might be to memset the who array to zero
before you start anyway.

/Bruce

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [dpdk-stable] [dpdk-dev] [PATCH v2] examples/vm_power_manager: fix string null termination
  2019-04-26 12:31     ` Bruce Richardson
@ 2019-04-26 12:47       ` Burakov, Anatoly
  0 siblings, 0 replies; 10+ messages in thread
From: Burakov, Anatoly @ 2019-04-26 12:47 UTC (permalink / raw)
  To: Bruce Richardson; +Cc: David Hunt, dev, stable

On 26-Apr-19 1:31 PM, Bruce Richardson wrote:
> On Fri, Apr 26, 2019 at 12:56:08PM +0100, Burakov, Anatoly wrote:
>> On 26-Apr-19 12:24 PM, David Hunt wrote:
>>> coverity complains about a null-termination after a read,
>>> so we terminate after exiting the do-while loop. The position
>>> is conditional on whether idx is within the buffer or at the
>>> end of the buffer.
>>>
>>> Coverity issue: 337680
>>> Fixes: a63504a90f ("examples/power: add JSON string handling")
>>> CC: stable@dpdk.org
>>>
>>> Signed-off-by: David Hunt <david.hunt@intel.com>
>>>
>>> ---
>>> v2:
>>>      * Move null termination outside of do-while.
>>> ---
>>>    examples/vm_power_manager/channel_monitor.c | 2 ++
>>>    1 file changed, 2 insertions(+)
>>>
>>> diff --git a/examples/vm_power_manager/channel_monitor.c b/examples/vm_power_manager/channel_monitor.c
>>> index 971e4f2bc..03fdcd15a 100644
>>> --- a/examples/vm_power_manager/channel_monitor.c
>>> +++ b/examples/vm_power_manager/channel_monitor.c
>>> @@ -822,6 +822,8 @@ read_json_packet(struct channel_info *chan_info)
>>>    				break;
>>>    		} while (indent > 0);
>>> +		json_data[idx + (idx < MAX_JSON_STRING_LEN - 1)] = '\0';
>>> +
>>
>> I don't think you need this complicated logic here. You start at idx = 0, so
>> even if you receive 0 bytes, you'll terminate buffer at index 0. You also
>> break when idx reaches (MAX_JSON_STRING_LEN - 1), so it's also safe to do
>> json_data[idx] after the loop. In all other cases, you still increment idx
>> before breaking out (e.g. when reaching indent == 0), so it's also safe to
>> do json_data[idx] in those cases.
>>
> +1 to that.
> 
> An alternative and simpler option might be to memset the who array to zero
> before you start anyway.

That'll cost us few extra cycles on a non-performance critical path full 
of syscalls, surely we can't have that! :)

> 
> /Bruce
> 


-- 
Thanks,
Anatoly

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [dpdk-stable] [PATCH v3] examples/vm_power_manager: fix string null termination
  2019-04-26 11:24 ` [dpdk-stable] [PATCH v2] " David Hunt
  2019-04-26 11:56   ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
@ 2019-04-26 14:04   ` David Hunt
  2019-04-26 14:10     ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
  1 sibling, 1 reply; 10+ messages in thread
From: David Hunt @ 2019-04-26 14:04 UTC (permalink / raw)
  To: dev; +Cc: david.hunt, stable

coverity complains about a null-termination after a read,
so we terminate once we exit the do-while read loop.

Coverity issue: 337680
Fixes: a63504a90f ("examples/power: add JSON string handling")
CC: stable@dpdk.org

Signed-off-by: David Hunt <david.hunt@intel.com>

---
v2: Move null termination outside of do-while.
v3: Simplify null termimation
---
 examples/vm_power_manager/channel_monitor.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/examples/vm_power_manager/channel_monitor.c b/examples/vm_power_manager/channel_monitor.c
index 971e4f2bc..4a287109b 100644
--- a/examples/vm_power_manager/channel_monitor.c
+++ b/examples/vm_power_manager/channel_monitor.c
@@ -822,12 +822,7 @@ read_json_packet(struct channel_info *chan_info)
 				break;
 		} while (indent > 0);
 
-		if (indent > 0)
-			/*
-			 * We've broken out of the read loop without getting
-			 * a closing brace, so throw away the data
-			 */
-			json_data[idx] = 0;
+		json_data[idx] = '\0';
 
 		if (strlen(json_data) == 0)
 			continue;
-- 
2.17.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [dpdk-stable] [dpdk-dev] [PATCH v3] examples/vm_power_manager: fix string null termination
  2019-04-26 14:04   ` [dpdk-stable] [PATCH v3] " David Hunt
@ 2019-04-26 14:10     ` Burakov, Anatoly
  2019-05-02 23:43       ` Thomas Monjalon
  0 siblings, 1 reply; 10+ messages in thread
From: Burakov, Anatoly @ 2019-04-26 14:10 UTC (permalink / raw)
  To: David Hunt, dev; +Cc: stable

On 26-Apr-19 3:04 PM, David Hunt wrote:
> coverity complains about a null-termination after a read,
> so we terminate once we exit the do-while read loop.
> 
> Coverity issue: 337680
> Fixes: a63504a90f ("examples/power: add JSON string handling")
> CC: stable@dpdk.org
> 
> Signed-off-by: David Hunt <david.hunt@intel.com>
> 
> ---
> v2: Move null termination outside of do-while.
> v3: Simplify null termimation
> ---
>   examples/vm_power_manager/channel_monitor.c | 7 +------
>   1 file changed, 1 insertion(+), 6 deletions(-)
> 
> diff --git a/examples/vm_power_manager/channel_monitor.c b/examples/vm_power_manager/channel_monitor.c
> index 971e4f2bc..4a287109b 100644
> --- a/examples/vm_power_manager/channel_monitor.c
> +++ b/examples/vm_power_manager/channel_monitor.c
> @@ -822,12 +822,7 @@ read_json_packet(struct channel_info *chan_info)
>   				break;
>   		} while (indent > 0);
>   
> -		if (indent > 0)
> -			/*
> -			 * We've broken out of the read loop without getting
> -			 * a closing brace, so throw away the data
> -			 */
> -			json_data[idx] = 0;
> +		json_data[idx] = '\0';
>   
>   		if (strlen(json_data) == 0)
>   			continue;
> 

Reviewed-by: Anatoly Burakov <anatoly.burakov@intel.com>

-- 
Thanks,
Anatoly

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [dpdk-stable] [dpdk-dev] [PATCH v3] examples/vm_power_manager: fix string null termination
  2019-04-26 14:10     ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
@ 2019-05-02 23:43       ` Thomas Monjalon
  0 siblings, 0 replies; 10+ messages in thread
From: Thomas Monjalon @ 2019-05-02 23:43 UTC (permalink / raw)
  To: David Hunt; +Cc: dev, Burakov, Anatoly, stable

26/04/2019 16:10, Burakov, Anatoly:
> On 26-Apr-19 3:04 PM, David Hunt wrote:
> > coverity complains about a null-termination after a read,
> > so we terminate once we exit the do-while read loop.
> > 
> > Coverity issue: 337680
> > Fixes: a63504a90f ("examples/power: add JSON string handling")
> > CC: stable@dpdk.org
> > 
> > Signed-off-by: David Hunt <david.hunt@intel.com>
> 
> Reviewed-by: Anatoly Burakov <anatoly.burakov@intel.com>

Applied, thanks

PS: replaced "string" with "json" in title to give a bit more context.




^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2019-05-02 23:43 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-26  8:43 [dpdk-stable] [PATCH v1] examples/vm_power_manager: fix string null termination David Hunt
2019-04-26 10:33 ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
2019-04-26 11:16   ` Hunt, David
2019-04-26 11:24 ` [dpdk-stable] [PATCH v2] " David Hunt
2019-04-26 11:56   ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
2019-04-26 12:31     ` Bruce Richardson
2019-04-26 12:47       ` Burakov, Anatoly
2019-04-26 14:04   ` [dpdk-stable] [PATCH v3] " David Hunt
2019-04-26 14:10     ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
2019-05-02 23:43       ` Thomas Monjalon

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).