diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 6787d12d5abf3fbb83b51850baed0ac4ecb19da7..16e2b719d47c2d22d76d905893efc8995e129b1d 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1985,6 +1985,10 @@ static inline int ext4_forced_shutdown(struct ext4_sb_info *sbi)
  * Structure of a directory entry
  */
 #define EXT4_NAME_LEN 255
+/*
+ * Base length of the ext4 directory entry excluding the name length
+ */
+#define EXT4_BASE_DIR_LEN (sizeof(struct ext4_dir_entry_2) - EXT4_NAME_LEN)
 
 struct ext4_dir_entry {
 	__le32	inode;			/* Inode number */
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 867deea2fbdb76042421fc41db6df4d7e0803f92..e3e57eab371f13f94d2e5f97ad6f92a7442063bd 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1304,10 +1304,10 @@ int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
 
 	de = (struct ext4_dir_entry_2 *)search_buf;
 	dlimit = search_buf + buf_size;
-	while ((char *) de < dlimit) {
+	while ((char *) de < dlimit - EXT4_BASE_DIR_LEN) {
 		/* this code is executed quadratically often */
 		/* do minimal checking `by hand' */
-		if ((char *) de + de->name_len <= dlimit &&
+		if (de->name + de->name_len <= dlimit &&
 		    ext4_match(fname, de)) {
 			/* found a match - just to be sure, do
 			 * a full check */