From edd5c230afdc7341e5a5564d9c8ee714688705fe Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Tue, 13 Apr 2021 21:33:13 -0400 Subject: [PATCH] Fix crash when input has no holes. When the search tree has no elements, it seems the CNearTreeObjects function does not give a CVector with zero elements but rather a null pointer. This results in a null dereference when that is passed to CVectorSize, instead of 0 as expected. Add an explicit check for this case to avoid crashing. --- src/slotifier.c | 4 ++++ tests/simple.at | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/slotifier.c b/src/slotifier.c index b74a9ec..03485f5 100644 --- a/src/slotifier.c +++ b/src/slotifier.c @@ -333,6 +333,9 @@ static int slotify(gerbv_image_t *drill) } CNearTreeObjects(t, &holes); + if (!holes) + goto out; + for (i = 0; i < CVectorSize(holes); i++) { gerbv_net_t *hole; @@ -358,6 +361,7 @@ static int slotify(gerbv_image_t *drill) gerbv_image_delete_net(hole); } +out: CNearTreeFree(&t); return ret; } diff --git a/tests/simple.at b/tests/simple.at index 8e28f6b..11c59b7 100644 --- a/tests/simple.at +++ b/tests/simple.at @@ -85,3 +85,28 @@ M30 ]]) AT_CLEANUP + +AT_SETUP([no holes]) + +AT_DATA([test.cnc], +[[M48 +INCH,TZ +T10C0.091 +% +T10 +X010512Y002362G85X010512Y002047 +M30 +]]) + +AT_CHECK([slotifier test.cnc], [0], +[[M48 +INCH,TZ +T10C0.091 +% +T10 +X010512Y002362G85X010512Y002047 +M30 + +]]) + +AT_CLEANUP -- 2.43.0