diff options
author | Gary E. Miller <gem@rellim.com> | 2016-05-25 16:47:02 -0700 |
---|---|---|
committer | Gary E. Miller <gem@rellim.com> | 2016-05-25 16:47:02 -0700 |
commit | edb950b99281dc6366ce1634cc8e73a5902a2d66 (patch) | |
tree | 9db147da7877eb92dbddd56f1d8b4d1e452292c2 | |
parent | 85893cd541159293e0a08ced8d04ab6f85813999 (diff) | |
download | gpsd-edb950b99281dc6366ce1634cc8e73a5902a2d66.tar.gz |
Replace a malloc() with an alloca()
This is to confrm with gpsd hacking standards which discourage malloc().
-rw-r--r-- | libgpsd_core.c | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/libgpsd_core.c b/libgpsd_core.c index 12655a55..3fef4a86 100644 --- a/libgpsd_core.c +++ b/libgpsd_core.c @@ -214,31 +214,26 @@ static void gpsd_run_device_hook(struct gpsd_errout_t *errout, "no %s present, skipped running %s hook\n", DEVICEHOOKPATH, hook); else { - /* - * We make an exception to the no-malloc rule here because - * the pointer will never persist outside this small scope - * and can thus never cause a leak or stale-pointer problem. + /* 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 = malloc(bufsize); - if (buf == NULL) - gpsd_log(errout, LOG_ERROR, - "error allocating run-hook buffer\n"); + 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", + DEVICEHOOKPATH, device_name, hook); + gpsd_log(errout, LOG_INF, "running %s\n", buf); + status = system(buf); + if (status == -1) + gpsd_log(errout, LOG_ERROR, "error running %s\n", buf); else - { - int status; - (void)snprintf(buf, bufsize, "%s %s %s", - DEVICEHOOKPATH, device_name, hook); - gpsd_log(errout, LOG_INF, "running %s\n", buf); - status = system(buf); - if (status == -1) - gpsd_log(errout, LOG_ERROR, "error running %s\n", buf); - else - gpsd_log(errout, LOG_INF, - "%s returned %d\n", DEVICEHOOKPATH, - WEXITSTATUS(status)); - free(buf); - } + gpsd_log(errout, LOG_INF, + "%s returned %d\n", DEVICEHOOKPATH, + WEXITSTATUS(status)); + /* buf automatically freed here as the stack pops */ } } |