@@ -67,26 +67,35 @@ static ssize_t romfs_mtd_strnlen(struct super_block *sb,
6767 * compare a string to one in a romfs image on MTD
6868 * - return 1 if matched, 0 if differ, -ve if error
6969 */
70- static int romfs_mtd_strncmp (struct super_block * sb , unsigned long pos ,
71- const char * str , size_t size )
70+ static int romfs_mtd_strcmp (struct super_block * sb , unsigned long pos ,
71+ const char * str , size_t size )
7272{
73- u_char buf [16 ];
73+ u_char buf [17 ];
7474 size_t len , segment ;
7575 int ret ;
7676
77- /* scan the string up to 16 bytes at a time */
77+ /* scan the string up to 16 bytes at a time, and attempt to grab the
78+ * trailing NUL whilst we're at it */
79+ buf [0 ] = 0xff ;
80+
7881 while (size > 0 ) {
79- segment = min_t (size_t , size , 16 );
82+ segment = min_t (size_t , size + 1 , 17 );
8083 ret = ROMFS_MTD_READ (sb , pos , segment , & len , buf );
8184 if (ret < 0 )
8285 return ret ;
86+ len -- ;
8387 if (memcmp (buf , str , len ) != 0 )
8488 return 0 ;
89+ buf [0 ] = buf [len ];
8590 size -= len ;
8691 pos += len ;
8792 str += len ;
8893 }
8994
95+ /* check the trailing NUL was */
96+ if (buf [0 ])
97+ return 0 ;
98+
9099 return 1 ;
91100}
92101#endif /* CONFIG_ROMFS_ON_MTD */
@@ -154,28 +163,48 @@ static ssize_t romfs_blk_strnlen(struct super_block *sb,
154163 * compare a string to one in a romfs image on a block device
155164 * - return 1 if matched, 0 if differ, -ve if error
156165 */
157- static int romfs_blk_strncmp (struct super_block * sb , unsigned long pos ,
158- const char * str , size_t size )
166+ static int romfs_blk_strcmp (struct super_block * sb , unsigned long pos ,
167+ const char * str , size_t size )
159168{
160169 struct buffer_head * bh ;
161170 unsigned long offset ;
162171 size_t segment ;
163- bool x ;
172+ bool matched , terminated = false ;
164173
165- /* scan the string up to 16 bytes at a time */
174+ /* compare string up to a block at a time */
166175 while (size > 0 ) {
167176 offset = pos & (ROMBSIZE - 1 );
168177 segment = min_t (size_t , size , ROMBSIZE - offset );
169178 bh = sb_bread (sb , pos >> ROMBSBITS );
170179 if (!bh )
171180 return - EIO ;
172- x = (memcmp (bh -> b_data + offset , str , segment ) != 0 );
173- brelse (bh );
174- if (x )
175- return 0 ;
181+ matched = (memcmp (bh -> b_data + offset , str , segment ) == 0 );
182+
176183 size -= segment ;
177184 pos += segment ;
178185 str += segment ;
186+ if (matched && size == 0 && offset + segment < ROMBSIZE ) {
187+ if (!bh -> b_data [offset + segment ])
188+ terminated = true;
189+ else
190+ matched = false;
191+ }
192+ brelse (bh );
193+ if (!matched )
194+ return 0 ;
195+ }
196+
197+ if (!terminated ) {
198+ /* the terminating NUL must be on the first byte of the next
199+ * block */
200+ BUG_ON ((pos & (ROMBSIZE - 1 )) != 0 );
201+ bh = sb_bread (sb , pos >> ROMBSBITS );
202+ if (!bh )
203+ return - EIO ;
204+ matched = !bh -> b_data [0 ];
205+ brelse (bh );
206+ if (!matched )
207+ return 0 ;
179208 }
180209
181210 return 1 ;
@@ -234,10 +263,12 @@ ssize_t romfs_dev_strnlen(struct super_block *sb,
234263
235264/*
236265 * compare a string to one in romfs
266+ * - the string to be compared to, str, may not be NUL-terminated; instead the
267+ * string is of the specified size
237268 * - return 1 if matched, 0 if differ, -ve if error
238269 */
239- int romfs_dev_strncmp (struct super_block * sb , unsigned long pos ,
240- const char * str , size_t size )
270+ int romfs_dev_strcmp (struct super_block * sb , unsigned long pos ,
271+ const char * str , size_t size )
241272{
242273 size_t limit ;
243274
@@ -246,16 +277,16 @@ int romfs_dev_strncmp(struct super_block *sb, unsigned long pos,
246277 return - EIO ;
247278 if (size > ROMFS_MAXFN )
248279 return - ENAMETOOLONG ;
249- if (size > limit - pos )
280+ if (size + 1 > limit - pos )
250281 return - EIO ;
251282
252283#ifdef CONFIG_ROMFS_ON_MTD
253284 if (sb -> s_mtd )
254- return romfs_mtd_strncmp (sb , pos , str , size );
285+ return romfs_mtd_strcmp (sb , pos , str , size );
255286#endif
256287#ifdef CONFIG_ROMFS_ON_BLOCK
257288 if (sb -> s_bdev )
258- return romfs_blk_strncmp (sb , pos , str , size );
289+ return romfs_blk_strcmp (sb , pos , str , size );
259290#endif
260291 return - EIO ;
261292}
0 commit comments