Use a reasonable-sized buffer when formatting error messages about

installing zoneinfo.  While we're in the vicinity, add some missing
error checking to eliminate an unhelpful error message when unlink()
fails.

/me is embarrassed by the quality of his 16-year-old code.
The whole thing is awful and could stand a complete rewrite.

PR:		164038
Submitted by:	Devin Teske (but implemented differently)
This commit is contained in:
Garrett Wollman 2012-01-12 05:50:32 +00:00
parent 8900f4b872
commit 8c261aeac3

View File

@ -57,6 +57,13 @@ __FBSDID("$FreeBSD$");
#define _PATH_DB "/var/db/zoneinfo"
#define _PATH_WALL_CMOS_CLOCK "/etc/wall_cmos_clock"
#ifdef PATH_MAX
#define SILLY_BUFFER_SIZE 2*PATH_MAX
#else
#warning "Somebody needs to fix this to dynamically size this buffer."
#define SILLY_BUFFER_SIZE 2048
#endif
/* special return codes for `fire' actions */
#define DITEM_FAILURE 1
@ -638,7 +645,7 @@ static int
install_zoneinfo_file(const char *zoneinfo_file)
{
char buf[1024];
char title[64], prompt[64];
char title[64], prompt[SILLY_BUFFER_SIZE];
struct stat sb;
ssize_t len;
int fd1, fd2, copymode;
@ -709,7 +716,18 @@ install_zoneinfo_file(const char *zoneinfo_file)
return (DITEM_FAILURE | DITEM_RECREATE);
}
unlink(path_localtime);
if (unlink(path_localtime) < 0) {
snprintf(prompt, sizeof(prompt),
"Could not unlink %s: %s",
path_localtime, strerror(errno));
if (usedialog) {
snprintf(title, sizeof(title), "Error");
dialog_msgbox(title, prompt, 8, 72, 1);
} else
fprintf(stderr, "%s\n", prompt);
return (DITEM_FAILURE | DITEM_RECREATE);
}
fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
S_IRUSR | S_IRGRP | S_IROTH);
if (fd2 < 0) {
@ -755,7 +773,17 @@ install_zoneinfo_file(const char *zoneinfo_file)
fprintf(stderr, "%s\n", prompt);
return (DITEM_FAILURE | DITEM_RECREATE);
}
unlink(path_localtime);
if (unlink(path_localtime) < 0) {
snprintf(prompt, sizeof(prompt),
"Could not unlink %s: %s",
path_localtime, strerror(errno));
if (usedialog) {
snprintf(title, sizeof(title), "Error");
dialog_msgbox(title, prompt, 8, 72, 1);
} else
fprintf(stderr, "%s\n", prompt);
return (DITEM_FAILURE | DITEM_RECREATE);
}
if (symlink(zoneinfo_file, path_localtime) < 0) {
snprintf(title, sizeof(title), "Error");
snprintf(prompt, sizeof(prompt),