[v1] examples/vm_power_manager: fix string null termination

Message ID 20190426084337.3921-1-david.hunt@intel.com (mailing list archive)
State Superseded, archived
Headers
Series [v1] examples/vm_power_manager: fix string null termination |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Hunt, David April 26, 2019, 8:43 a.m. UTC
  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(+)
  

Comments

Anatoly Burakov April 26, 2019, 10:33 a.m. UTC | #1
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] == '{')
>
  
Hunt, David April 26, 2019, 11:16 a.m. UTC | #2
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] == '{')
>>
>
>
  

Patch

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] == '{')