[dpdk-dev] ivshmem: fix segmentation fault in corner case

Message ID 1434630450-10497-1-git-send-email-sergio.gonzalez.monroy@intel.com (mailing list archive)
State Accepted, archived
Headers

Commit Message

Sergio Gonzalez Monroy June 18, 2015, 12:27 p.m. UTC
  Depending on the configured segments it is possible to hit a
segmentation fault as a result of decrementing an unsigned index with
value 0.

To avoid it, exit the loop if the index has value 0.

Signed-off-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
---
 lib/librte_ivshmem/rte_ivshmem.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Thomas Monjalon June 22, 2015, 4:40 p.m. UTC | #1
2015-06-18 13:27, Sergio Gonzalez Monroy:
> Depending on the configured segments it is possible to hit a
> segmentation fault as a result of decrementing an unsigned index with
> value 0.
> 
> To avoid it, exit the loop if the index has value 0.
> 
> Signed-off-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>

Applied, thanks
  

Patch

diff --git a/lib/librte_ivshmem/rte_ivshmem.c b/lib/librte_ivshmem/rte_ivshmem.c
index 7ca55ed..9621906 100644
--- a/lib/librte_ivshmem/rte_ivshmem.c
+++ b/lib/librte_ivshmem/rte_ivshmem.c
@@ -377,6 +377,8 @@  build_config(struct rte_ivshmem_metadata * metadata)
 			for (j = biggest_idx - 1; j >= i; j--) {
 				memcpy(&pages[j+1], &pages[j], sizeof(struct rte_memseg));
 				memset(&pages[j], 0, sizeof(struct rte_memseg));
+				if (j == 0)
+					break;
 			}
 
 			/* put old biggest segment to its new place */