Skip to content

Commit 5c936ce

Browse files
jerpeleagregory-nutt
authored andcommitted
Various fixes (#6)
Author: Gregory Nutt <[email protected]> Run all .c and .h affected by this PR through nxstyle. Author: Alin Jerpelea <[email protected]> * system/usbmsc: Fix accessing uninitialized pointer * fsutils/inifile: Fix a memory leak in inifile error case * fsutils/mksmartfs: Fix uninitialized return code * system/zmodem: Fix a compile error in zmodem debug enabled * nshlib/nsh_fscmds.c: Add syntax check to cp command If the destication of NutShell cp command is the same with the source, it may cause the file corruption. Add the syntax check of argument to avoid this problem.
1 parent 74ba8ff commit 5c936ce

File tree

5 files changed

+99
-62
lines changed

5 files changed

+99
-62
lines changed

fsutils/inifile/inifile.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ struct inifile_state_s
106106
{
107107
FILE *instream;
108108
int nextch;
109-
char line[CONFIG_FSUTILS_INIFILE_MAXLINE+1];
109+
char line[CONFIG_FSUTILS_INIFILE_MAXLINE + 1];
110110
};
111111

112112
/****************************************************************************
@@ -185,7 +185,7 @@ static int inifile_read_line(FAR struct inifile_state_s *priv)
185185
while ((nbytes < CONFIG_FSUTILS_INIFILE_MAXLINE) &&
186186
(priv->nextch != EOF) &&
187187
(priv->nextch != '\n'))
188-
{
188+
{
189189
/* Always ignore carriage returns */
190190

191191
if (priv->nextch != '\r')
@@ -242,7 +242,8 @@ static int inifile_read_noncomment_line(FAR struct inifile_state_s *priv)
242242

243243
/* Read until either a (1) no further lines are found in
244244
* the file, or (2) a line that does not begin with a semi-colon
245-
* is found */
245+
* is found.
246+
*/
246247

247248
do nbytes = inifile_read_line(priv);
248249
while (nbytes > 0 && priv->line[0] == ';');
@@ -354,7 +355,7 @@ static bool inifile_read_variable(FAR struct inifile_state_s *priv,
354355
* the section is found, or (3) a valid variable assignment is found.
355356
*/
356357

357-
for (;;)
358+
for (; ; )
358359
{
359360
/* Read the next line in the buffer */
360361

@@ -365,9 +366,9 @@ static bool inifile_read_variable(FAR struct inifile_state_s *priv,
365366
*/
366367

367368
if (!nbytes || priv->line[0] == '[')
368-
{
369-
return false;
370-
}
369+
{
370+
return false;
371+
}
371372

372373
/* Search for the '=' delimiter. NOTE the line is guaranteed to
373374
* be NULL terminated by inifile_read_noncomment_line().
@@ -389,7 +390,7 @@ static bool inifile_read_variable(FAR struct inifile_state_s *priv,
389390
* a NULL string
390391
*/
391392

392-
varinfo->variable = (char*)priv->line;
393+
varinfo->variable = (FAR char *)priv->line;
393394
varinfo->value = (ptr + 1);
394395
return true;
395396
}
@@ -418,7 +419,7 @@ static FAR char *
418419

419420
iniinfo("variable=\"%s\"\n", variable);
420421

421-
for (;;)
422+
for (; ; )
422423
{
423424
/* Get the next variable from this section. */
424425

@@ -527,6 +528,7 @@ INIHANDLE inifile_initialize(FAR const char *inifile_name)
527528
else
528529
{
529530
inidbg("ERROR: Could not open \"%s\"\n", inifile_name);
531+
free(priv);
530532
return (INIHANDLE)NULL;
531533
}
532534
}

fsutils/mksmartfs/mksmartfs.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
int issmartfs(FAR const char *pathname)
9696
{
9797
struct smart_format_s fmt;
98-
int ret, fd;
98+
int fd;
99+
int ret;
99100

100101
/* Find the inode of the block driver identified by 'source' */
101102

@@ -121,6 +122,7 @@ int issmartfs(FAR const char *pathname)
121122
}
122123

123124
out:
125+
124126
/* Close the driver */
125127

126128
close(fd);
@@ -157,22 +159,25 @@ int issmartfs(FAR const char *pathname)
157159
****************************************************************************/
158160

159161
#ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS
160-
int mksmartfs(FAR const char *pathname, uint16_t sectorsize, uint8_t nrootdirs)
162+
int mksmartfs(FAR const char *pathname, uint16_t sectorsize,
163+
uint8_t nrootdirs)
161164
#else
162165
int mksmartfs(FAR const char *pathname, uint16_t sectorsize)
163166
#endif
164167
{
165168
struct smart_format_s fmt;
166-
int ret, fd;
167-
int x;
168-
uint8_t type;
169169
struct smart_read_write_s request;
170+
uint8_t type;
171+
int fd;
172+
int x;
173+
int ret;
170174

171175
/* Find the inode of the block driver indentified by 'source' */
172176

173177
fd = open(pathname, O_RDWR);
174178
if (fd < 0)
175179
{
180+
ret = -ENOENT;
176181
goto errout;
177182
}
178183

@@ -227,11 +232,13 @@ int mksmartfs(FAR const char *pathname, uint16_t sectorsize)
227232
}
228233

229234
errout_with_driver:
235+
230236
/* Close the driver */
231237

232238
close(fd);
233239

234240
errout:
241+
235242
/* Release all allocated memory */
236243

237244
/* Return any reported errors */

nshlib/nsh_fscmds.c

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
#include <errno.h>
5757
#include <debug.h>
5858

59-
6059
#if !defined(CONFIG_DISABLE_MOUNTPOINT)
6160
# ifdef CONFIG_FS_READABLE /* Need at least one filesytem in configuration */
6261
# include <sys/mount.h>
@@ -240,47 +239,47 @@ static int ls_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
240239

241240
if ((buf.st_mode & S_IRUSR) != 0)
242241
{
243-
details[1]='r';
242+
details[1] = 'r';
244243
}
245244

246245
if ((buf.st_mode & S_IWUSR) != 0)
247246
{
248-
details[2]='w';
247+
details[2] = 'w';
249248
}
250249

251250
if ((buf.st_mode & S_IXUSR) != 0)
252251
{
253-
details[3]='x';
252+
details[3] = 'x';
254253
}
255254

256255
if ((buf.st_mode & S_IRGRP) != 0)
257256
{
258-
details[4]='r';
257+
details[4] = 'r';
259258
}
260259

261260
if ((buf.st_mode & S_IWGRP) != 0)
262261
{
263-
details[5]='w';
262+
details[5] = 'w';
264263
}
265264

266265
if ((buf.st_mode & S_IXGRP) != 0)
267266
{
268-
details[6]='x';
267+
details[6] = 'x';
269268
}
270269

271270
if ((buf.st_mode & S_IROTH) != 0)
272271
{
273-
details[7]='r';
272+
details[7] = 'r';
274273
}
275274

276275
if ((buf.st_mode & S_IWOTH) != 0)
277276
{
278-
details[8]='w';
277+
details[8] = 'w';
279278
}
280279

281280
if ((buf.st_mode & S_IXOTH) != 0)
282281
{
283-
details[9]='x';
282+
details[9] = 'x';
284283
}
285284

286285
nsh_output(vtbl, " %s", details);
@@ -561,7 +560,7 @@ int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
561560

562561
/* Construct the full path to the new file */
563562

564-
allocpath = nsh_getdirpath(vtbl, destpath, basename(argv[1]) );
563+
allocpath = nsh_getdirpath(vtbl, destpath, basename(argv[1]));
565564
if (!allocpath)
566565
{
567566
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
@@ -581,6 +580,14 @@ int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
581580
}
582581
}
583582

583+
/* Check if the destination does not match the source */
584+
585+
if (strcmp(destpath, srcpath) == 0)
586+
{
587+
nsh_error(vtbl, g_fmtsyntax, argv[0]);
588+
goto errout_with_allocpath;
589+
}
590+
584591
/* Now open the destination */
585592

586593
wrfd = open(destpath, oflags, 0666);
@@ -592,7 +599,7 @@ int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
592599

593600
/* Now copy the file */
594601

595-
for (;;)
602+
for (; ; )
596603
{
597604
int nbytesread;
598605
int nbyteswritten;
@@ -644,9 +651,10 @@ int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
644651
}
645652
else
646653
{
647-
/* Read error */
654+
/* Read error */
648655

649-
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "write", NSH_ERRNO);
656+
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "write",
657+
NSH_ERRNO);
650658
}
651659

652660
goto errout_with_wrfd;
@@ -678,6 +686,7 @@ int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
678686
{
679687
nsh_freefullpath(srcpath);
680688
}
689+
681690
errout:
682691
return ret;
683692
}
@@ -862,7 +871,8 @@ int cmd_losmart(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
862871
/* Get the losetup options: Two forms are supported:
863872
*
864873
* losmart -d <loop-device>
865-
* losmart [-m minor-number] [-o <offset>] [-e erasesize] [-s sectsize] [-r] <filename>
874+
* losmart [-m minor-number] [-o <offset>] [-e erasesize] [-s sectsize]
875+
* [-r] <filename>
866876
*
867877
* NOTE that the -o and -r options are accepted with the -d option, but
868878
* will be ignored.
@@ -955,7 +965,8 @@ int cmd_losmart(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
955965
{
956966
/* Tear down the loop device. */
957967

958-
ret = ioctl(fd, SMART_LOOPIOC_TEARDOWN, (unsigned long)((uintptr_t) loopdev));
968+
ret = ioctl(fd, SMART_LOOPIOC_TEARDOWN,
969+
(unsigned long)((uintptr_t) loopdev));
959970
if (ret < 0)
960971
{
961972
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
@@ -973,7 +984,8 @@ int cmd_losmart(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
973984
setup.offset = offset; /* An offset that may be applied to the device */
974985
setup.readonly = readonly; /* True: Read access will be supported only */
975986

976-
ret = ioctl(fd, SMART_LOOPIOC_SETUP, (unsigned long)((uintptr_t)&setup));
987+
ret = ioctl(fd, SMART_LOOPIOC_SETUP,
988+
(unsigned long)((uintptr_t)&setup));
977989
if (ret < 0)
978990
{
979991
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
@@ -1091,7 +1103,7 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
10911103
switch (option)
10921104
{
10931105
case 'l':
1094-
lsflags |= (LSFLAGS_SIZE|LSFLAGS_LONG);
1106+
lsflags |= (LSFLAGS_SIZE | LSFLAGS_LONG);
10951107
break;
10961108

10971109
case 'R':
@@ -1169,7 +1181,8 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
11691181
* file
11701182
*/
11711183

1172-
ret = ls_handler(vtbl, fullpath, NULL, (FAR void *)((uintptr_t)lsflags));
1184+
ret = ls_handler(vtbl, fullpath, NULL,
1185+
(FAR void *)((uintptr_t)lsflags));
11731186
}
11741187
else
11751188
{
@@ -1178,7 +1191,7 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
11781191
nsh_output(vtbl, "%s:\n", fullpath);
11791192

11801193
ret = nsh_foreach_direntry(vtbl, "ls", fullpath, ls_handler,
1181-
(FAR void*)((uintptr_t)lsflags));
1194+
(FAR void *)((uintptr_t)lsflags));
11821195
if (ret == OK && (lsflags & LSFLAGS_RECURSIVE) != 0)
11831196
{
11841197
/* Then recurse to list each directory within the directory */
@@ -1282,7 +1295,7 @@ int cmd_mkfatfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
12821295

12831296
/* There should be exactly one parameter left on the command-line */
12841297

1285-
if (optind == argc-1)
1298+
if (optind == argc - 1)
12861299
{
12871300
fullpath = nsh_getfullpath(vtbl, argv[optind]);
12881301
if (fullpath == NULL)
@@ -1480,7 +1493,8 @@ int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
14801493
nsh_error(vtbl, "Sector size must be 256-16384\n");
14811494
return EINVAL;
14821495
}
1483-
if (sectorsize & (sectorsize-1))
1496+
1497+
if (sectorsize & (sectorsize - 1))
14841498
{
14851499
nsh_error(vtbl, "Sector size must be power of 2\n");
14861500
return EINVAL;
@@ -1523,7 +1537,8 @@ int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
15231537
#endif
15241538
if (ret < 0)
15251539
{
1526-
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "mksmartfs", NSH_ERRNO);
1540+
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "mksmartfs",
1541+
NSH_ERRNO);
15271542
}
15281543
}
15291544

@@ -1594,6 +1609,7 @@ int cmd_readlink(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
15941609
ssize_t len;
15951610

15961611
/* readlink <link> */
1612+
15971613
/* Get the fullpath to the directory */
15981614

15991615
fullpath = nsh_getfullpath(vtbl, argv[1]);
@@ -1734,7 +1750,7 @@ int cmd_cmp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
17341750
* files.
17351751
*/
17361752

1737-
for (;;)
1753+
for (; ; )
17381754
{
17391755
char buf1[128];
17401756
char buf2[128];

0 commit comments

Comments
 (0)