Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 991922c

Browse files
committed
container.c: Fix warnings of "format-overflow=" with new GCC
Change sprintf to snprintf and check the return values of snprintf to handle this warning. Fixes: #364 Signed-off-by: Hui Zhu <[email protected]>
1 parent c0c07d2 commit 991922c

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/container.c

+24-6
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ static int container_setup_volume(struct hyper_pod *pod,
146146
if (hyper_mount_nfs(vol->device, path) < 0)
147147
return -1;
148148
/* nfs export has implicitly included _data part of the volume */
149-
sprintf(volume, "/%s/", path);
149+
if (snprintf(volume, 512, "/%s/", path) < strlen("//")) {
150+
fprintf(stderr, "something wrong with volume generation\n");
151+
return -1;
152+
}
150153
} else {
151154
fprintf(stdout, "mount %s to %s, tmp path %s\n",
152155
dev, vol->mountpoint, path);
@@ -164,7 +167,10 @@ static int container_setup_volume(struct hyper_pod *pod,
164167
perror("mount volume device failed");
165168
return -1;
166169
}
167-
sprintf(volume, "/%s/_data", path);
170+
if (snprintf(volume, 512, "/%s/_data", path) < strlen("/_data")) {
171+
fprintf(stderr, "something wrong with volume generation\n");
172+
return -1;
173+
}
168174
}
169175

170176
if (container_check_volume(volume, &filevolume, &newvolume) < 0)
@@ -193,7 +199,10 @@ static int container_setup_volume(struct hyper_pod *pod,
193199
perror("create volume file failed");
194200
return -1;
195201
}
196-
sprintf(volume, "/%s/_data/%s", path, filevolume);
202+
if (snprintf(volume, 512, "/%s/_data/%s", path, filevolume) < strlen(path)) {
203+
fprintf(stderr, "something wrong with volume generation\n");
204+
return -1;
205+
}
197206
/* 0777 so that any user can read/write the new file volume */
198207
if (chmod(volume, 0777) < 0) {
199208
fprintf(stderr, "fail to chmod directory %s\n", volume);
@@ -235,7 +244,10 @@ static int container_setup_volume(struct hyper_pod *pod,
235244
}
236245
if (map->docker) {
237246
/* converted from volume */
238-
sprintf(volume, "%s/_data", path);
247+
if (snprintf(volume, 512, "%s/_data", path) < strlen("/_data")) {
248+
fprintf(stderr, "something wrong with volume generation\n");
249+
return -1;
250+
}
239251
src = volume;
240252
if (container->initialize &&
241253
(container_populate_volume(mountpoint, volume) < 0)) {
@@ -279,7 +291,10 @@ static int container_setup_modules(struct hyper_container *container)
279291
}
280292

281293
sprintf(src, "/lib/modules/%s", uts.release);
282-
sprintf(dst, "./%s", src);
294+
if (snprintf(dst, 512, "./%s", src) < 3) {
295+
fprintf(stderr, "something wrong with dst generation\n");
296+
return -1;
297+
}
283298

284299
if (stat(dst, &st) == 0) {
285300
struct dirent **list;
@@ -657,7 +672,10 @@ static int hyper_setup_container_rootfs(void *data)
657672
fprintf(stdout, "root directory for container is %s/%s, init task %s\n",
658673
root, container->rootfs, container->exec.argv[0]);
659674

660-
sprintf(rootfs, "%s/%s/", root, container->rootfs);
675+
if (snprintf(rootfs, 512, "%s/%s/", root, container->rootfs) < 2) {
676+
fprintf(stderr, "something wrong with rootfs generation\n");
677+
goto fail;
678+
}
661679
if (mount(rootfs, rootfs, NULL, MS_BIND|MS_REC, NULL) < 0) {
662680
perror("failed to bind rootfs");
663681
goto fail;

0 commit comments

Comments
 (0)