summaryrefslogtreecommitdiffstats
path: root/sys-utils/hwclock.c
diff options
context:
space:
mode:
authorSami Kerola2016-07-17 13:12:52 +0200
committerSami Kerola2017-02-05 00:39:37 +0100
commit4aca5fe247738d43920c6ea20030848d7864aba2 (patch)
tree18150060e385e5487b93ac52af8c5290dca4d3a4 /sys-utils/hwclock.c
parenthwclock: remove dead code and other minor fixes (diff)
downloadkernel-qcow2-util-linux-4aca5fe247738d43920c6ea20030848d7864aba2.tar.gz
kernel-qcow2-util-linux-4aca5fe247738d43920c6ea20030848d7864aba2.tar.xz
kernel-qcow2-util-linux-4aca5fe247738d43920c6ea20030848d7864aba2.zip
hwclock: simplify save_adjtime() execution flow
Return early to avoid excessive nesting. In same go remove any chance of overflow by using appropriate allocation. And update variable names to be easier to understand. Reviewed-by: J William Piggott <elseifthen@gmx.com> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'sys-utils/hwclock.c')
-rw-r--r--sys-utils/hwclock.c82
1 files changed, 34 insertions, 48 deletions
diff --git a/sys-utils/hwclock.c b/sys-utils/hwclock.c
index b08bd781a..c5be96f54 100644
--- a/sys-utils/hwclock.c
+++ b/sys-utils/hwclock.c
@@ -82,6 +82,7 @@
#include "hwclock.h"
#include "timeutils.h"
#include "env.h"
+#include "xalloc.h"
#ifdef HAVE_LIBAUDIT
#include <libaudit.h>
@@ -1076,57 +1077,42 @@ calculate_adjustment(const struct hwclock_control *ctl,
static void save_adjtime(const struct hwclock_control *ctl,
const struct adjtime *adjtime)
{
- char newfile[412]; /* Stuff to write to disk file */
+ char *content; /* Stuff to write to disk file */
+ FILE *fp;
+ int err = 0;
- if (adjtime->dirty) {
- /*
- * snprintf is not always available, but this is safe as
- * long as libc does not use more than 100 positions for %ld
- * or %f
- */
- sprintf(newfile, "%f %ld %f\n%ld\n%s\n",
- adjtime->drift_factor,
- adjtime->last_adj_time,
- adjtime->not_adjusted,
- adjtime->last_calib_time,
- (adjtime->local_utc == LOCAL) ? "LOCAL" : "UTC");
+ if (!adjtime->dirty)
+ return;
- if (ctl->testing) {
- printf(_
- ("Not updating adjtime file because of testing mode.\n"));
- printf(_("Would have written the following to %s:\n%s"),
- ctl->adj_file_name, newfile);
- } else {
- FILE *adjfile;
- int err = 0;
-
- adjfile = fopen(ctl->adj_file_name, "w");
- if (adjfile == NULL) {
- warn(_
- ("Could not open file with the clock adjustment parameters "
- "in it (%s) for writing"), ctl->adj_file_name);
- err = 1;
- } else {
- if (fputs(newfile, adjfile) < 0) {
- warn(_
- ("Could not update file with the clock adjustment "
- "parameters (%s) in it"),
- ctl->adj_file_name);
- err = 1;
- }
- if (close_stream(adjfile) != 0) {
- warn(_
- ("Could not update file with the clock adjustment "
- "parameters (%s) in it"),
- ctl->adj_file_name);
- err = 1;
- }
- }
- if (err)
- warnx(_
- ("Drift adjustment parameters not updated."));
- }
+ xasprintf(&content, "%f %ld %f\n%ld\n%s\n",
+ adjtime->drift_factor,
+ adjtime->last_adj_time,
+ adjtime->not_adjusted,
+ adjtime->last_calib_time,
+ (adjtime->local_utc == LOCAL) ? "LOCAL" : "UTC");
+
+ if (ctl->testing) {
+ printf(_
+ ("Not updating adjtime file because of testing mode.\n"));
+ printf(_("Would have written the following to %s:\n%s"),
+ ctl->adj_file_name, content);
+ free(content);
+ return;
+ }
+
+ fp = fopen(ctl->adj_file_name, "w");
+ if (fp == NULL) {
+ warn(_("Could not open file with the clock adjustment parameters "
+ "in it (%s) for writing"), ctl->adj_file_name);
+ err = 1;
+ } else if (fputs(content, fp) < 0 || close_stream(fp) != 0) {
+ warn(_("Could not update file with the clock adjustment "
+ "parameters (%s) in it"), ctl->adj_file_name);
+ err = 1;
}
+ free(content);
+ if (err)
+ warnx(_("Drift adjustment parameters not updated."));
}
/*