Skip to contentSkip to conversation

examples/vm_power_manager: fix string null termination

PATCH Superseded David Hunt 4 participants 10 messages Apr 26, 2019 – May 2, 2019

lore
Share & tools
lore

David Hunt

author
#0 lore raw

PATCH 1/1 examples/vm_power_manager: fix string null termination

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")
---
 examples/vm_power_manager/channel_monitor.c | 1 +
 1 file changed, 1 insertion(+)
examples/vm_power_manager/channel_monitor.c +1 -0
index 971e4f2bc..711722fef 100644
@@ -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] == '{')
Signature · 1 line
2.17.1
#1 lore raw
Quoted 17 lines · David Hunt

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")
---
  examples/vm_power_manager/channel_monitor.c | 1 +
  1 file changed, 1 insertion(+)
examples/vm_power_manager/channel_monitor.c +0 -0
index 971e4f2bc..711722fef 100644

examples/vm_power_manager/channel_monitor.c read_json_packet(struct channel_info *chan_info) · line 811

Quoted 4 lines
examples/vm_power_manager/channel_monitor.c +1 -0
@@ -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?

Quoted 4 lines
examples/vm_power_manager/channel_monitor.c +0 -0
  			if (n_bytes == 0)
  				break;
  			if (json_data[idx] == '{')
Signature · 2 lines
Thanks,
Anatoly
#2 lore raw

Hi Anatoly,

Quoted 13 lines · Anatoly
Quoted 12 lines · David Hunt

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

---
  examples/vm_power_manager/channel_monitor.c | 1 +

  1 file changed, 1 insertion(+)
Quoted 12 lines

examples/vm_power_manager/channel_monitor.c read_json_packet(struct channel_info *chan_info) · line 811

Quoted 9 lines
examples/vm_power_manager/channel_monitor.c +1 -0
index 971e4f2bc..711722fef 100644
@@ -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.

Quoted 6 lines
Quoted 4 lines
examples/vm_power_manager/channel_monitor.c +0 -0
               if (n_bytes == 0)
                   break;
               if (json_data[idx] == '{')
author
#3 lore raw

PATCH v2 1/1 examples/vm_power_manager: fix string null termination

In reply to David Hunt

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

---
v2:
* Move null termination outside of do-while.

---
 examples/vm_power_manager/channel_monitor.c | 2 ++
 1 file changed, 2 insertions(+)
examples/vm_power_manager/channel_monitor.c +2 -0
index 971e4f2bc..03fdcd15a 100644
@@ -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
Signature · 1 line
2.17.1
#4 lore raw
Quoted 23 lines · David Hunt

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

---
v2:
* Move null termination outside of do-while.

---
  examples/vm_power_manager/channel_monitor.c | 2 ++
  1 file changed, 2 insertions(+)
examples/vm_power_manager/channel_monitor.c +0 -0
index 971e4f2bc..03fdcd15a 100644

examples/vm_power_manager/channel_monitor.c read_json_packet(struct channel_info *chan_info) · line 826

Quoted 5 lines
examples/vm_power_manager/channel_monitor.c +2 -0
@@ -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.

Quoted 4 lines
examples/vm_power_manager/channel_monitor.c +0 -0
  		if (indent > 0)
  			/*
  			 * We've broken out of the read loop without getting
Signature · 2 lines
Thanks,
Anatoly
#5 lore raw
Quoted 19 lines · Anatoly
Quoted 18 lines · David Hunt

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

---
v2:
* Move null termination outside of do-while.

---
  examples/vm_power_manager/channel_monitor.c | 2 ++
  1 file changed, 2 insertions(+)
Quoted 17 lines

examples/vm_power_manager/channel_monitor.c read_json_packet(struct channel_info *chan_info) · line 825

Quoted 9 lines
examples/vm_power_manager/channel_monitor.c +2 -0
index 971e4f2bc..03fdcd15a 100644
@@ -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

#6 lore raw
Quoted 37 lines · Bruce Richardson
Quoted 19 lines · Anatoly
Quoted 18 lines · David Hunt

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

---
v2:
* Move null termination outside of do-while.

---
   examples/vm_power_manager/channel_monitor.c | 2 ++
   1 file changed, 2 insertions(+)
Quoted 17 lines

examples/vm_power_manager/channel_monitor.c read_json_packet(struct channel_info *chan_info) · line 825

Quoted 9 lines
examples/vm_power_manager/channel_monitor.c +2 -0
index 971e4f2bc..03fdcd15a 100644
@@ -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.

Quoted 4 lines

+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! :)

Quoted 3 lines

/Bruce

Signature · 2 lines
Thanks,
Anatoly
author
#7 lore raw

PATCH v3 1/1 examples/vm_power_manager: fix string null termination

In reply to David Hunt

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

---
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(-)
examples/vm_power_manager/channel_monitor.c +1 -6
index 971e4f2bc..4a287109b 100644
@@ -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;
Signature · 1 line
2.17.1
Reviewed-by on v3 1/1
#8 lore raw
Quoted 25 lines · David Hunt

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

---
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(-)
examples/vm_power_manager/channel_monitor.c +0 -0
index 971e4f2bc..4a287109b 100644
@@ -822,12 +822,7 @@ read_json_packet(struct channel_info *chan_info)
  				break;
  		} while (indent > 0);
 

examples/vm_power_manager/channel_monitor.c read_json_packet(struct channel_info *chan_info) · line 828

Quoted 10 lines
examples/vm_power_manager/channel_monitor.c +1 -6
-		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;
Signature · 2 lines
Thanks,
Anatoly
#9 lore raw

26/04/2019 16:10, Burakov, Anatoly:

Quoted 11 lines · David Hunt
Quoted 8 lines

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

Applied, thanks

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