Skip to content
Snippets Groups Projects
Commit 767c807f authored by Tomasz Grabiec's avatar Tomasz Grabiec
Browse files

ramfs: fix rename when new name is not longer


strlcpy() expects buffer length not string length.

Signed-off-by: default avatarTomasz Grabiec <tgrabiec@cloudius-systems.com>
parent 1989d9ea
No related branches found
No related tags found
No related merge requests found
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
/&/tests/tst-dns-resolver.so: ./& /&/tests/tst-dns-resolver.so: ./&
/&/tests/tst-fs-link.so: ./& /&/tests/tst-fs-link.so: ./&
/&/tests/tst-kill.so: ./& /&/tests/tst-kill.so: ./&
/&/tests/tst-rename.so: ./&
/&/tests/bench/bench.jar: ./& /&/tests/bench/bench.jar: ./&
/testrunner.so: ./tests/testrunner.so /testrunner.so: ./tests/testrunner.so
/java/Hello.class: ./tests/hello/Hello.class /java/Hello.class: ./tests/hello/Hello.class
......
...@@ -157,6 +157,7 @@ tests += tests/tst-loadbalance.so ...@@ -157,6 +157,7 @@ tests += tests/tst-loadbalance.so
tests += tests/tst-dns-resolver.so tests += tests/tst-dns-resolver.so
tests += tests/tst-fs-link.so tests += tests/tst-fs-link.so
tests += tests/tst-kill.so tests += tests/tst-kill.so
tests += tests/tst-rename.so
tests/hello/Hello.class: javabase=tests/hello tests/hello/Hello.class: javabase=tests/hello
......
...@@ -143,7 +143,7 @@ ramfs_rename_node(struct ramfs_node *np, char *name) ...@@ -143,7 +143,7 @@ ramfs_rename_node(struct ramfs_node *np, char *name)
len = strlen(name); len = strlen(name);
if (len <= np->rn_namelen) { if (len <= np->rn_namelen) {
/* Reuse current name buffer */ /* Reuse current name buffer */
strlcpy(np->rn_name, name, np->rn_namelen); strlcpy(np->rn_name, name, np->rn_namelen + 1);
} else { } else {
/* Expand name buffer */ /* Expand name buffer */
tmp = malloc(len + 1); tmp = malloc(len + 1);
......
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
static const char *SECRET = "Hello, world";
static bool has_secret(const char *name) {
FILE * file = fopen(name, "r");
if (!file) {
perror("Failed to open");
return 1;
}
char buf[strlen(SECRET) + 1];
fgets(buf, sizeof(buf), file);
if (strcmp(SECRET, buf)) {
fprintf(stderr, "File content does not match, "
"expected %s but found %s, file=%s\n", SECRET, buf, file);
fclose(file);
return false;
}
fclose(file);
return true;
}
static int do_rename(const char *src, const char *dst)
{
printf("Renaming %s to %s\n", src, dst);
if (rename(src, dst)) {
perror("Failed to rename.");
return 1;
}
if (!has_secret(dst)) {
return 1;
}
return 0;
}
static int write_secret(const char *file_name) {
printf("Writing secret to: %s\n", file_name);
FILE * file = fopen(file_name, "w");
if (!file) {
perror("Failed to open");
return 1;
}
fputs(SECRET, file);
fclose(file);
return 0;
}
int main(int argc, char const *argv[])
{
const char * src_name = strdup(tmpnam(NULL));
if (write_secret(src_name))
return 1;
const char * dst_name = strdup(tmpnam(NULL));
if (do_rename(src_name, dst_name))
return 1;
const char *short_name = "a";
if (do_rename(dst_name, short_name))
return 1;
if (do_rename(short_name, src_name))
return 1;
printf("OK.\n");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment