Skip to content

Commit b80697c

Browse files
committed
udf: Remove declarations of arrays of size UDF_NAME_LEN (256 bytes)
There are several places in UDF where we declared temporary arrays of UDF_NAME_LEN bytes on stack. This is not nice to stack usage so this patch changes those places to use kmalloc() instead. Also clean up bail-out paths in those functions when we are changing them. Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 9bf2c6b commit b80697c

2 files changed

Lines changed: 93 additions & 92 deletions

File tree

‎fs/udf/dir.c‎

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
static int do_udf_readdir(struct inode *dir, struct file *filp,
4040
filldir_t filldir, void *dirent)
4141
{
42-
struct udf_fileident_bh fibh;
42+
struct udf_fileident_bh fibh = { .sbh = NULL, .ebh = NULL};
4343
struct fileIdentDesc *fi = NULL;
4444
struct fileIdentDesc cfi;
4545
int block, iblock;
4646
loff_t nf_pos = (filp->f_pos - 1) << 2;
4747
int flen;
48-
char fname[UDF_NAME_LEN];
48+
char *fname = NULL;
4949
char *nameptr;
5050
uint16_t liu;
5151
uint8_t lfi;
@@ -54,23 +54,32 @@ static int do_udf_readdir(struct inode *dir, struct file *filp,
5454
kernel_lb_addr eloc;
5555
uint32_t elen;
5656
sector_t offset;
57-
int i, num;
57+
int i, num, ret = 0;
5858
unsigned int dt_type;
5959
struct extent_position epos = { NULL, 0, {0, 0} };
6060
struct udf_inode_info *iinfo;
6161

6262
if (nf_pos >= size)
63-
return 0;
63+
goto out;
64+
65+
fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
66+
if (!fname) {
67+
ret = -ENOMEM;
68+
goto out;
69+
}
6470

6571
if (nf_pos == 0)
6672
nf_pos = udf_ext0_offset(dir);
6773

6874
fibh.soffset = fibh.eoffset = nf_pos & (dir->i_sb->s_blocksize - 1);
6975
iinfo = UDF_I(dir);
70-
if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
71-
fibh.sbh = fibh.ebh = NULL;
72-
} else if (inode_bmap(dir, nf_pos >> dir->i_sb->s_blocksize_bits,
73-
&epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
76+
if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
77+
if (inode_bmap(dir, nf_pos >> dir->i_sb->s_blocksize_bits,
78+
&epos, &eloc, &elen, &offset)
79+
!= (EXT_RECORDED_ALLOCATED >> 30)) {
80+
ret = -ENOENT;
81+
goto out;
82+
}
7483
block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
7584
if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
7685
if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
@@ -83,8 +92,8 @@ static int do_udf_readdir(struct inode *dir, struct file *filp,
8392
}
8493

8594
if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
86-
brelse(epos.bh);
87-
return -EIO;
95+
ret = -EIO;
96+
goto out;
8897
}
8998

9099
if (!(offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9)) - 1))) {
@@ -105,23 +114,15 @@ static int do_udf_readdir(struct inode *dir, struct file *filp,
105114
brelse(bha[i]);
106115
}
107116
}
108-
} else {
109-
brelse(epos.bh);
110-
return -ENOENT;
111117
}
112118

113119
while (nf_pos < size) {
114120
filp->f_pos = (nf_pos >> 2) + 1;
115121

116122
fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
117123
&elen, &offset);
118-
if (!fi) {
119-
if (fibh.sbh != fibh.ebh)
120-
brelse(fibh.ebh);
121-
brelse(fibh.sbh);
122-
brelse(epos.bh);
123-
return 0;
124-
}
124+
if (!fi)
125+
goto out;
125126

126127
liu = le16_to_cpu(cfi.lengthOfImpUse);
127128
lfi = cfi.lengthFileIdent;
@@ -167,25 +168,21 @@ static int do_udf_readdir(struct inode *dir, struct file *filp,
167168
dt_type = DT_UNKNOWN;
168169
}
169170

170-
if (flen) {
171-
if (filldir(dirent, fname, flen, filp->f_pos, iblock, dt_type) < 0) {
172-
if (fibh.sbh != fibh.ebh)
173-
brelse(fibh.ebh);
174-
brelse(fibh.sbh);
175-
brelse(epos.bh);
176-
return 0;
177-
}
178-
}
171+
if (flen && filldir(dirent, fname, flen, filp->f_pos,
172+
iblock, dt_type) < 0)
173+
goto out;
179174
} /* end while */
180175

181176
filp->f_pos = (nf_pos >> 2) + 1;
182177

178+
out:
183179
if (fibh.sbh != fibh.ebh)
184180
brelse(fibh.ebh);
185181
brelse(fibh.sbh);
186182
brelse(epos.bh);
183+
kfree(fname);
187184

188-
return 0;
185+
return ret;
189186
}
190187

191188
static int udf_readdir(struct file *filp, void *dirent, filldir_t filldir)

0 commit comments

Comments
 (0)