diff --git a/svr-authpubkey.c b/svr-authpubkey.c
index 4eca2110337cf2800cf6746014701895096d0e22..66fe5e5f4ff1a0c4e57073fa4f74ba1bded64ca6 100644
--- a/svr-authpubkey.c
+++ b/svr-authpubkey.c
@@ -294,8 +294,8 @@ static int checkpubkey(unsigned char* algo, unsigned int algolen,
 			options_buf = buf_new(options_len);
 			buf_putbytes(options_buf, options_start, options_len);
 
-			/* compare the algorithm */
-			if (line->pos + algolen > line->len) {
+			/* compare the algorithm. +3 so we have enough bytes to read a space and some base64 characters too. */
+			if (line->pos + algolen+3 > line->len) {
 				continue;
 			}
 			if (strncmp(buf_getptr(line, algolen), algo, algolen) != 0) {