maximilian attems
2011-Jul-05 18:06 UTC
[klibc] [PATCH 1/7] ln: Check snprintf() return values
Add some semi-useful error message, as printing the failing dir or file
seems not really advisable after that error.
Signed-off-by: maximilian attems <max at stro.at>
---
usr/utils/ln.c | 20 +++++++++++++++-----
1 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/usr/utils/ln.c b/usr/utils/ln.c
index e826eb8..257b33f 100644
--- a/usr/utils/ln.c
+++ b/usr/utils/ln.c
@@ -9,7 +9,7 @@
int main(int argc, char *argv[])
{
- int c, s, f;
+ int c, s, f, len;
char *p;
struct stat sb;
@@ -56,10 +56,20 @@ int main(int argc, char *argv[])
p = strrchr(argv[c], '/');
p++;
- if (S_ISDIR(sb.st_mode))
- snprintf(target, PATH_MAX, "%s/%s", argv[argc - 1], p);
- else
- snprintf(target, PATH_MAX, "%s", argv[argc - 1]);
+ if (S_ISDIR(sb.st_mode)) {
+ len = snprintf(target, PATH_MAX, "%s/%s",
+ argv[argc - 1], p);
+ if (len >= PATH_MAX) {
+ fprintf(stderr, "snprintf directory failed");
+ return 1;
+ }
+ } else {
+ len = snprintf(target, PATH_MAX, "%s", argv[argc - 1]);
+ if (len >= PATH_MAX) {
+ fprintf(stderr, "snprintf file failed");
+ return -1;
+ }
+ }
if (f)
unlink(target);
--
1.7.5.4
maximilian attems
2011-Jul-05 18:06 UTC
[klibc] [PATCH 2/7] kinit: try_name() check snprintf return
Signed-off-by: maximilian attems <max at stro.at> --- usr/kinit/name_to_dev.c | 8 ++++++-- 1 files changed, 6 insertions(+), 2 deletions(-) diff --git a/usr/kinit/name_to_dev.c b/usr/kinit/name_to_dev.c index d491285..8604375 100644 --- a/usr/kinit/name_to_dev.c +++ b/usr/kinit/name_to_dev.c @@ -26,7 +26,9 @@ static dev_t try_name(char *name, int part) int fd; /* read device number from /sys/block/.../dev */ - snprintf(path, sizeof(path), "/sys/block/%s/dev", name); + len = snprintf(path, sizeof(path), "/sys/block/%s/dev", name); + if (len >= sizeof(path)) + goto fail; fd = open(path, 0, 0); if (fd < 0) goto fail; @@ -49,7 +51,9 @@ static dev_t try_name(char *name, int part) return res; /* otherwise read range from .../range */ - snprintf(path, sizeof(path), "/sys/block/%s/range", name); + len = snprintf(path, sizeof(path), "/sys/block/%s/range", name); + if (len >= sizeof(path)) + goto fail; fd = open(path, 0, 0); if (fd < 0) goto fail; -- 1.7.5.4
maximilian attems
2011-Jul-05 18:06 UTC
[klibc] [PATCH 3/7] fstpye: no need for braces around return values
Signed-off-by: maximilian attems <max at stro.at>
---
usr/kinit/fstype/fstype.c | 14 +++++++-------
1 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/usr/kinit/fstype/fstype.c b/usr/kinit/fstype/fstype.c
index 451d60c..9f0f931 100644
--- a/usr/kinit/fstype/fstype.c
+++ b/usr/kinit/fstype/fstype.c
@@ -124,7 +124,7 @@ static int fs_proc_check(const char *fs_name)
f = fopen("/proc/filesystems", "r");
if (!f)
- return (0);
+ return 0;
while (fgets(buf, sizeof(buf), f)) {
cp = buf;
if (!isspace(*cp)) {
@@ -141,11 +141,11 @@ static int fs_proc_check(const char *fs_name)
*t = 0;
if (!strcmp(fs_name, cp)) {
fclose(f);
- return (1);
+ return 1;
}
}
fclose(f);
- return (0);
+ return 0;
}
/*
@@ -160,12 +160,12 @@ static int check_for_modules(const char *fs_name)
int i;
if (uname(&uts))
- return (0);
+ return 0;
snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep",
uts.release);
f = fopen(buf, "r");
if (!f)
- return (0);
+ return 0;
while (fgets(buf, sizeof(buf), f)) {
if ((cp = strchr(buf, ':')) != NULL)
*cp = 0;
@@ -181,11 +181,11 @@ static int check_for_modules(const char *fs_name)
}
if (!strcmp(cp, fs_name)) {
fclose(f);
- return (1);
+ return 1;
}
}
fclose(f);
- return (0);
+ return 0;
}
static int base_ext4_image(const void *buf, unsigned long long *bytes,
--
1.7.5.4
maximilian attems
2011-Jul-05 18:06 UTC
[klibc] [PATCH 4/7] fstype: check_for_modules() check snprintf() return value
Signed-off-by: maximilian attems <max at stro.at> --- usr/kinit/fstype/fstype.c | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-) diff --git a/usr/kinit/fstype/fstype.c b/usr/kinit/fstype/fstype.c index 9f0f931..3f8dac0 100644 --- a/usr/kinit/fstype/fstype.c +++ b/usr/kinit/fstype/fstype.c @@ -161,7 +161,10 @@ static int check_for_modules(const char *fs_name) if (uname(&uts)) return 0; - snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release); + i = snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", + uts.release); + if (i >= sizeof(buf)) + return 0; f = fopen(buf, "r"); if (!f) -- 1.7.5.4
maximilian attems
2011-Jul-05 18:06 UTC
[klibc] [PATCH 5/7] ipconfig: dump_device_config() check return of snprintf()
Signed-off-by: maximilian attems <max at stro.at>
---
usr/kinit/ipconfig/main.c | 5 ++++-
1 files changed, 4 insertions(+), 1 deletions(-)
diff --git a/usr/kinit/ipconfig/main.c b/usr/kinit/ipconfig/main.c
index 6ad5588..8004ad5 100644
--- a/usr/kinit/ipconfig/main.c
+++ b/usr/kinit/ipconfig/main.c
@@ -123,8 +123,11 @@ static void dump_device_config(struct netdev *dev)
{
char fn[40];
FILE *f;
+ int len;
- snprintf(fn, sizeof(fn), "/tmp/net-%s.conf", dev->name);
+ len = snprintf(fn, sizeof(fn), "/tmp/net-%s.conf", dev->name);
+ if (len >= sizeof(fn))
+ return;
f = fopen(fn, "w");
if (f) {
write_option(f, "DEVICE", dev->name);
--
1.7.5.4
maximilian attems
2011-Jul-05 18:06 UTC
[klibc] [PATCH 6/7] strsignal(): Check snprintf() return value
return NULL if snprintf() call fails.
strsignal(3) manpage seems wrong on that account.
Signed-off-by: maximilian attems <max at stro.at>
---
usr/klibc/strsignal.c | 9 +++++++--
1 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/usr/klibc/strsignal.c b/usr/klibc/strsignal.c
index e345e9c..b89b8d0 100644
--- a/usr/klibc/strsignal.c
+++ b/usr/klibc/strsignal.c
@@ -9,18 +9,23 @@
char *strsignal(int sig)
{
static char buf[64];
+ int len;
if ((unsigned)sig < _NSIG && sys_siglist[sig])
return (char *)sys_siglist[sig];
#ifdef SIGRTMIN
if (sig >= SIGRTMIN && sig <= SIGRTMAX) {
- snprintf(buf, sizeof buf, "Real-time signal %d",
+ len = snprintf(buf, sizeof buf, "Real-time signal %d",
sig - SIGRTMIN);
+ if (len >= sizeof buf)
+ return NULL;
return buf;
}
#endif
- snprintf(buf, sizeof buf, "Signal %d", sig);
+ len = snprintf(buf, sizeof buf, "Signal %d", sig);
+ if (len >= sizeof buf)
+ return NULL;
return buf;
}
--
1.7.5.4
maximilian attems
2011-Jul-05 18:06 UTC
[klibc] [PATCH 7/7] tests getoptlong check return value of snprintf()
Signed-off-by: maximilian attems <max at stro.at>
---
usr/klibc/tests/getoptlong.c | 6 ++++--
1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/usr/klibc/tests/getoptlong.c b/usr/klibc/tests/getoptlong.c
index 6c019b7..8e969b8 100644
--- a/usr/klibc/tests/getoptlong.c
+++ b/usr/klibc/tests/getoptlong.c
@@ -27,7 +27,7 @@ int main(int argc, char *const *argv)
const char *showchar;
char one_char[] = "\'?\'";
char num_buf[16];
- int c;
+ int c, len;
int longindex;
parser = getenv("GETOPTTEST");
@@ -43,7 +43,9 @@ int main(int argc, char *const *argv)
one_char[1] = c;
showchar = one_char;
} else {
- snprintf(num_buf, sizeof num_buf, "%d", c);
+ len = snprintf(num_buf, sizeof num_buf, "%d", c);
+ if (len >= sizeof num_buf)
+ return 0;
showchar = num_buf;
}
--
1.7.5.4