Replace sprintf calls with snprintf

To quiet diet-libc

Signed-off-by: Neil Brown <neilb@cse.unsw.edu.au>
This commit is contained in:
Neil Brown 2005-06-14 06:42:13 +00:00
parent 173fc51539
commit 8f23b0b3fa
5 changed files with 15 additions and 13 deletions

View File

@ -20,6 +20,7 @@ Changes Prior to this release
This is because the device list can change and so is not a
stable aspect of the array
- Allow --force with --grow so '-Gfn1' works (on raid1)
- Replace sprintf calls with snprintf (to quiet diet-libc)
Changes Prior to 1.11.0 release
- Fix embarassing bug which causes --add to always fail.

View File

@ -66,7 +66,7 @@ const char *human_chunksize(unsigned long bytes)
i++;
}
sprintf(buf, "%lu %s", bytes, suffixes[i]);
snprintf(buf, sizeof(buf), "%lu %s", bytes, suffixes[i]);
return buf;
}

View File

@ -42,7 +42,8 @@ void make_parts(char *dev, int cnt)
struct stat stb;
int major, minor;
int i;
char *name = malloc(strlen(dev) + 20);
int nlen = strlen(dev) + 20;
char *name = malloc(nlen);
int dig = isdigit(dev[strlen(dev)-1]);
if (stat(dev, &stb)!= 0)
@ -53,7 +54,7 @@ void make_parts(char *dev, int cnt)
minor = minor(stb.st_rdev);
for (i=1; i <= cnt ; i++) {
struct stat stb2;
sprintf(name, "%s%s%d", dev, dig?"p":"", i);
snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i);
if (stat(name, &stb2)==0) {
if (!S_ISBLK(stb2.st_mode))
continue;

View File

@ -117,7 +117,7 @@ static void examine_super0(void *sbv)
char nb[5];
if (d>=0) dp = &sb->disks[d];
else dp = &sb->this_disk;
sprintf(nb, "%4d", d);
snprintf(nb, sizeof(nb), "%4d", d);
printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb,
dp->number, dp->major, dp->minor, dp->raid_disk);
if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");

18
util.c
View File

@ -427,13 +427,13 @@ char *human_size(long long bytes)
else if (bytes < 2*1024LL*1024LL*1024LL) {
long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
cMiB/100 , cMiB % 100,
cMB/100, cMB % 100);
} else {
long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
cGiB/100 , cGiB % 100,
cGB/100, cGB % 100);
}
@ -446,16 +446,16 @@ char *human_size_brief(long long bytes)
if (bytes < 5000*1024)
sprintf(buf, "%ld.%02ldKiB",
snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
(long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
);
else if (bytes < 2*1024LL*1024LL*1024LL)
sprintf(buf, "%ld.%02ldMiB",
snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
(long)(bytes>>20),
(long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
);
else
sprintf(buf, "%ld.%02ldGiB",
snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
(long)(bytes>>30),
(long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
);
@ -505,20 +505,20 @@ char *get_md_name(int dev)
int mdp = get_mdp_major();
if (mdp < 0) return NULL;
rdev = makedev(mdp, (-1-dev)<<6);
sprintf(devname, "/dev/md/d%d", -1-dev);
snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
if (stat(devname, &stb) == 0
&& (S_IFMT&stb.st_mode) == S_IFBLK
&& (stb.st_rdev == rdev))
return devname;
} else {
rdev = makedev(MD_MAJOR, dev);
sprintf(devname, "/dev/md%d", dev);
snprintf(devname, sizeof(devname), "/dev/md%d", dev);
if (stat(devname, &stb) == 0
&& (S_IFMT&stb.st_mode) == S_IFBLK
&& (stb.st_rdev == rdev))
return devname;
sprintf(devname, "/dev/md/%d", dev);
snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
if (stat(devname, &stb) == 0
&& (S_IFMT&stb.st_mode) == S_IFBLK
&& (stb.st_rdev == rdev))
@ -527,7 +527,7 @@ char *get_md_name(int dev)
dn = map_dev(major(rdev), minor(rdev));
if (dn)
return dn;
sprintf(devname, "/dev/.tmp.md%d", dev);
snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
if (errno != EEXIST)
return NULL;