bej_decoder: Add a limit on number of operations

This is to prevent the decoder from taking on a huge number of
operations from a single input, that could cause the main while loop for
bejDecode to run for a long time.

Tested: Unit test passes

Change-Id: Idb68b706d3634d1874d1814f17420076672d21ee
Signed-off-by: Brandon Kim <brandonkim@google.com>
diff --git a/src/bej_decoder_core.c b/src/bej_decoder_core.c
index a9bb02c..b500fcf 100644
--- a/src/bej_decoder_core.c
+++ b/src/bej_decoder_core.c
@@ -719,8 +719,16 @@
         .stackDataPtr = stackDataPtr,
     };
 
+    uint64_t maxOperations = 1000000;
+    uint64_t operationCount = 0;
+
     while (params.state.encodedStreamOffset < streamLen)
     {
+        if (++operationCount > maxOperations)
+        {
+            fprintf(stderr, "BEJ decoding exceeded max operations\n");
+            return bejErrorNotSuppoted;
+        }
         // Go to the next encoded segment in the encoded stream.
         params.state.encodedSubStream =
             enStream + params.state.encodedStreamOffset;