diff --git a/minijson.pyx b/minijson.pyx
index 4fde182c10eeffaed9f0037f2e335c7ff60c8245..3408cbfbdf39b9be6df3e627a054944bf1c6e3ce 100644
--- a/minijson.pyx
+++ b/minijson.pyx
@@ -313,7 +313,9 @@ cpdef int dump(object data, cio: io.BytesIO) except -1:
         return 1
     elif isinstance(data, str):
         length = len(data)
-        if length < 128:
+        if length < 0:
+            raise EncodingError('Invalid length!')
+        elif length < 128:
             cio.write(bytearray([0x80 | length]))
             cio.write(data.encode('utf-8'))
             return 1+length