ptp4l and phc2sys: Get argument values with strict error checking

Signed-off-by: Ken ICHIKAWA <ichikawa.ken@jp.fujitsu.com>
master
Ken ICHIKAWA 2013-06-04 14:01:04 +09:00 committed by Richard Cochran
parent 42cb449eaa
commit 74e703727e
2 changed files with 40 additions and 12 deletions

View File

@ -19,6 +19,9 @@
*/ */
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <poll.h> #include <poll.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -28,7 +31,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <inttypes.h>
#include <linux/pps.h> #include <linux/pps.h>
#include <linux/ptp_clock.h> #include <linux/ptp_clock.h>
@ -576,7 +578,7 @@ int main(int argc, char *argv[])
int c, domain_number = 0, phc_readings = 5, pps_fd = -1; int c, domain_number = 0, phc_readings = 5, pps_fd = -1;
int max_ppb, r, wait_sync = 0, forced_sync_offset = 0; int max_ppb, r, wait_sync = 0, forced_sync_offset = 0;
int print_level = LOG_INFO, use_syslog = 1, verbose = 0; int print_level = LOG_INFO, use_syslog = 1, verbose = 0;
double ppb, phc_interval = 1.0; double ppb, phc_interval = 1.0, phc_rate;
struct timespec phc_interval_tp; struct timespec phc_interval_tp;
struct clock dst_clock = { struct clock dst_clock = {
.clkid = CLOCK_REALTIME, .clkid = CLOCK_REALTIME,
@ -611,39 +613,63 @@ int main(int argc, char *argv[])
src = clock_open(optarg); src = clock_open(optarg);
break; break;
case 'P': case 'P':
configured_pi_kp = atof(optarg); if (get_arg_val_d(c, optarg, &configured_pi_kp,
0.0, DBL_MAX))
return -1;
break; break;
case 'I': case 'I':
configured_pi_ki = atof(optarg); if (get_arg_val_d(c, optarg, &configured_pi_ki,
0.0, DBL_MAX))
return -1;
break; break;
case 'S': case 'S':
configured_pi_offset = atof(optarg); if (get_arg_val_d(c, optarg, &configured_pi_offset,
0.0, DBL_MAX))
return -1;
break; break;
case 'R': case 'R':
phc_interval = 1.0 / atof(optarg); if (get_arg_val_d(c, optarg, &phc_rate, 0.0, DBL_MAX))
return -1;
phc_interval = 1.0 / phc_rate;
/* phc_interval will be assigned to a time_t variable. */
/* check if that occurs overflow. */
if ((sizeof(time_t) == 8 && phc_interval > INT64_MAX) ||
(sizeof(time_t) == 4 && phc_interval > INT32_MAX)) {
fprintf(stderr,
"-R: %s is too small\n", optarg);
return -1;
}
break; break;
case 'N': case 'N':
phc_readings = atoi(optarg); if (get_arg_val_i(c, optarg, &phc_readings, 1, INT_MAX))
return -1;
break; break;
case 'O': case 'O':
dst_clock.sync_offset = atoi(optarg); if (get_arg_val_i(c, optarg, &dst_clock.sync_offset,
INT_MIN, INT_MAX))
return -1;
dst_clock.sync_offset_direction = -1; dst_clock.sync_offset_direction = -1;
forced_sync_offset = 1; forced_sync_offset = 1;
break; break;
case 'u': case 'u':
dst_clock.stats_max_count = atoi(optarg); if (get_arg_val_ui(c, optarg, &dst_clock.stats_max_count,
0, UINT_MAX))
return -1;
break; break;
case 'w': case 'w':
wait_sync = 1; wait_sync = 1;
break; break;
case 'n': case 'n':
domain_number = atoi(optarg); if (get_arg_val_i(c, optarg, &domain_number, 0, 255))
return -1;
break; break;
case 'x': case 'x':
dst_clock.kernel_leap = 0; dst_clock.kernel_leap = 0;
break; break;
case 'l': case 'l':
print_level = atoi(optarg); if (get_arg_val_i(c, optarg, &print_level,
PRINT_LEVEL_MIN, PRINT_LEVEL_MAX))
return -1;
break; break;
case 'm': case 'm':
verbose = 1; verbose = 1;

View File

@ -237,7 +237,9 @@ int main(int argc, char *argv[])
*cfg_ignore |= CFG_IGNORE_SLAVEONLY; *cfg_ignore |= CFG_IGNORE_SLAVEONLY;
break; break;
case 'l': case 'l':
cfg_settings.print_level = atoi(optarg); if (get_arg_val_i(c, optarg, &cfg_settings.print_level,
PRINT_LEVEL_MIN, PRINT_LEVEL_MAX))
return -1;
*cfg_ignore |= CFG_IGNORE_PRINT_LEVEL; *cfg_ignore |= CFG_IGNORE_PRINT_LEVEL;
break; break;
case 'm': case 'm':