diff options
author | Fred Wright <fw@fwright.net> | 2016-09-06 16:51:08 -0700 |
---|---|---|
committer | Gary E. Miller <gem@rellim.com> | 2016-09-06 16:51:08 -0700 |
commit | 56c7f38ea6f67748238571028a3bf6a3bebb3eb3 (patch) | |
tree | 3bd8f152845d7a1ca7c378ad0b96f65f1ed91397 /libgpsd_core.c | |
parent | 5808aa4caf5b03643a9ab9f110ae99010d8b4ded (diff) | |
download | gpsd-56c7f38ea6f67748238571028a3bf6a3bebb3eb3.tar.gz |
Eliminate the one use of the portability-challenged alloca()
In general, the use of alloca() is discouraged. Here, there was
only one use, with a maximum size determinable at compile time,
so the code has been reworked to use an ordinary array (without
relying on the C99 variable-length array feature). This always
allows for a GPS_PATH_MAX-sized device_name, but the amount of
space needed for that is relatively modest, and the behavior is
more reproducible with a fixed allocation.
TESTED:
Ran "scons build-all check" on OSX. This code shouldn't be
OS-dependent.
Signed-off-by: Gary E. Miller <gem@rellim.com>
Diffstat (limited to 'libgpsd_core.c')
-rw-r--r-- | libgpsd_core.c | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/libgpsd_core.c b/libgpsd_core.c index 377674cf..302b9cc8 100644 --- a/libgpsd_core.c +++ b/libgpsd_core.c @@ -218,25 +218,27 @@ const char *gpsd_prettydump(struct gps_device_t *session) } + +/* Define the possible hook strings here so we can get the length */ +#define HOOK_ACTIVATE "ACTIVATE" +#define HOOK_DEACTIVATE "DEACTIVATE" + +#define HOOK_CMD_MAX (sizeof(DEVICEHOOKPATH) + GPS_PATH_MAX \ + + sizeof(HOOK_DEACTIVATE)) + static void gpsd_run_device_hook(struct gpsd_errout_t *errout, char *device_name, char *hook) { struct stat statbuf; + char buf[HOOK_CMD_MAX]; + if (stat(DEVICEHOOKPATH, &statbuf) == -1) gpsd_log(errout, LOG_PROG, "no %s present, skipped running %s hook\n", DEVICEHOOKPATH, hook); else { - /* use alloca(), which is not malloc(), here because - * the pointer will never persist outside this small scope. - */ - size_t bufsize = strlen(DEVICEHOOKPATH) + 1 + strlen(device_name) + 1 + strlen(hook) + 1; - char *buf = alloca(bufsize); - /* no need to check the return code of alloca() - * by definition, if alloca() fails the program segfaults - */ int status; - (void)snprintf(buf, bufsize, "%s %s %s", + (void)snprintf(buf, sizeof(buf), "%s %s %s", DEVICEHOOKPATH, device_name, hook); gpsd_log(errout, LOG_INF, "running %s\n", buf); status = system(buf); @@ -246,7 +248,6 @@ static void gpsd_run_device_hook(struct gpsd_errout_t *errout, gpsd_log(errout, LOG_INF, "%s returned %d\n", DEVICEHOOKPATH, WEXITSTATUS(status)); - /* buf automatically freed here as the stack pops */ } } @@ -369,7 +370,7 @@ void gpsd_deactivate(struct gps_device_t *session) if (session->mode == O_OPTIMIZE) gpsd_run_device_hook(&session->context->errout, session->gpsdata.dev.path, - "DEACTIVATE"); + HOOK_DEACTIVATE); #ifdef PPS_ENABLE session->pps_thread.report_hook = NULL; /* tell any PPS-watcher thread to die */ #endif /* PPS_ENABLE */ @@ -556,7 +557,7 @@ int gpsd_activate(struct gps_device_t *session, const int mode) { if (mode == O_OPTIMIZE) gpsd_run_device_hook(&session->context->errout, - session->gpsdata.dev.path, "ACTIVATE"); + session->gpsdata.dev.path, HOOK_ACTIVATE); session->gpsdata.gps_fd = gpsd_open(session); if (mode != O_CONTINUE) session->mode = mode; |