mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-04 13:29:59 -05:00
core-util: replace remaining fixed size destination string functions by _malloc() versions
This helps portability to GNU/Hurd. Patch originally from Samuel Thibault but modified. Closes ticket #546
This commit is contained in:
parent
c6ea9fecc9
commit
49fd8ee72e
7 changed files with 119 additions and 38 deletions
|
|
@ -36,6 +36,7 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <pulse/util.h>
|
#include <pulse/util.h>
|
||||||
|
#include <pulse/xmalloc.h>
|
||||||
#include <pulsecore/core-error.h>
|
#include <pulsecore/core-error.h>
|
||||||
#include <pulsecore/core-util.h>
|
#include <pulsecore/core-util.h>
|
||||||
#include <pulsecore/log.h>
|
#include <pulsecore/log.h>
|
||||||
|
|
@ -147,47 +148,46 @@ int pa_authkey_load(const char *path, void *data, size_t length) {
|
||||||
|
|
||||||
/* If the specified file path starts with / return it, otherwise
|
/* If the specified file path starts with / return it, otherwise
|
||||||
* return path prepended with home directory */
|
* return path prepended with home directory */
|
||||||
static const char *normalize_path(const char *fn, char *s, size_t l) {
|
static char *normalize_path(const char *fn) {
|
||||||
|
|
||||||
pa_assert(fn);
|
pa_assert(fn);
|
||||||
pa_assert(s);
|
|
||||||
pa_assert(l > 0);
|
|
||||||
|
|
||||||
#ifndef OS_IS_WIN32
|
#ifndef OS_IS_WIN32
|
||||||
if (fn[0] != '/') {
|
if (fn[0] != '/') {
|
||||||
#else
|
#else
|
||||||
if (strlen(fn) < 3 || !isalpha(fn[0]) || fn[1] != ':' || fn[2] != '\\') {
|
if (strlen(fn) < 3 || !isalpha(fn[0]) || fn[1] != ':' || fn[2] != '\\') {
|
||||||
#endif
|
#endif
|
||||||
char homedir[PATH_MAX];
|
char *homedir, *s;
|
||||||
|
|
||||||
if (!pa_get_home_dir(homedir, sizeof(homedir)))
|
if (!(homedir = pa_get_home_dir_malloc()))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
#ifndef OS_IS_WIN32
|
s = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", homedir, fn);
|
||||||
pa_snprintf(s, l, "%s/%s", homedir, fn);
|
pa_xfree(homedir);
|
||||||
#else
|
|
||||||
pa_snprintf(s, l, "%s\\%s", homedir, fn);
|
|
||||||
#endif
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
return fn;
|
return pa_xstrdup(fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Load a cookie from a file in the home directory. If the specified
|
/* Load a cookie from a file in the home directory. If the specified
|
||||||
* path starts with /, use it as absolute path instead. */
|
* path starts with /, use it as absolute path instead. */
|
||||||
int pa_authkey_load_auto(const char *fn, void *data, size_t length) {
|
int pa_authkey_load_auto(const char *fn, void *data, size_t length) {
|
||||||
char path[PATH_MAX];
|
char *p;
|
||||||
const char *p;
|
int ret;
|
||||||
|
|
||||||
pa_assert(fn);
|
pa_assert(fn);
|
||||||
pa_assert(data);
|
pa_assert(data);
|
||||||
pa_assert(length > 0);
|
pa_assert(length > 0);
|
||||||
|
|
||||||
if (!(p = normalize_path(fn, path, sizeof(path))))
|
if (!(p = normalize_path(fn)))
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
return pa_authkey_load(p, data, length);
|
ret = pa_authkey_load(p, data, length);
|
||||||
|
pa_xfree(p);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Store the specified cookie in the specified cookie file */
|
/* Store the specified cookie in the specified cookie file */
|
||||||
|
|
@ -195,14 +195,13 @@ int pa_authkey_save(const char *fn, const void *data, size_t length) {
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
int unlock = 0, ret = -1;
|
int unlock = 0, ret = -1;
|
||||||
ssize_t r;
|
ssize_t r;
|
||||||
char path[PATH_MAX];
|
char *p;
|
||||||
const char *p;
|
|
||||||
|
|
||||||
pa_assert(fn);
|
pa_assert(fn);
|
||||||
pa_assert(data);
|
pa_assert(data);
|
||||||
pa_assert(length > 0);
|
pa_assert(length > 0);
|
||||||
|
|
||||||
if (!(p = normalize_path(fn, path, sizeof(path))))
|
if (!(p = normalize_path(fn)))
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
if ((fd = open(p, O_RDWR|O_CREAT|O_NOCTTY, S_IRUSR|S_IWUSR)) < 0) {
|
if ((fd = open(p, O_RDWR|O_CREAT|O_NOCTTY, S_IRUSR|S_IWUSR)) < 0) {
|
||||||
|
|
@ -232,5 +231,7 @@ finish:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pa_xfree(p);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -494,13 +494,14 @@ int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
|
||||||
struct dirent *e;
|
struct dirent *e;
|
||||||
|
|
||||||
while ((e = readdir(dir))) {
|
while ((e = readdir(dir))) {
|
||||||
char p[PATH_MAX];
|
char *p;
|
||||||
|
|
||||||
if (e->d_name[0] == '.')
|
if (e->d_name[0] == '.')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
pa_snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);
|
p = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", pathname, e->d_name);
|
||||||
add_file(c, p);
|
add_file(c, p);
|
||||||
|
pa_xfree(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
|
||||||
|
|
@ -1336,26 +1336,32 @@ int pa_unlock_lockfile(const char *fn, int fd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *get_pulse_home(void) {
|
static char *get_pulse_home(void) {
|
||||||
char h[PATH_MAX];
|
char *h;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
char *ret = NULL;
|
||||||
|
|
||||||
if (!pa_get_home_dir(h, sizeof(h))) {
|
if (!(h = pa_get_home_dir_malloc())) {
|
||||||
pa_log_error("Failed to get home directory.");
|
pa_log_error("Failed to get home directory.");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stat(h, &st) < 0) {
|
if (stat(h, &st) < 0) {
|
||||||
pa_log_error("Failed to stat home directory %s: %s", h, pa_cstrerror(errno));
|
pa_log_error("Failed to stat home directory %s: %s", h, pa_cstrerror(errno));
|
||||||
return NULL;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st.st_uid != getuid()) {
|
if (st.st_uid != getuid()) {
|
||||||
pa_log_error("Home directory %s not ours.", h);
|
pa_log_error("Home directory %s not ours.", h);
|
||||||
errno = EACCES;
|
errno = EACCES;
|
||||||
return NULL;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
return pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse", h);
|
ret = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse", h);
|
||||||
|
|
||||||
|
finish:
|
||||||
|
pa_xfree(h);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *pa_get_state_dir(void) {
|
char *pa_get_state_dir(void) {
|
||||||
|
|
@ -1380,6 +1386,50 @@ char *pa_get_state_dir(void) {
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *pa_get_home_dir_malloc(void) {
|
||||||
|
char *homedir;
|
||||||
|
size_t allocated = 128;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
homedir = pa_xmalloc(allocated);
|
||||||
|
|
||||||
|
if (!pa_get_home_dir(homedir, allocated)) {
|
||||||
|
pa_xfree(homedir);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strlen(homedir) < allocated - 1)
|
||||||
|
break;
|
||||||
|
|
||||||
|
pa_xfree(homedir);
|
||||||
|
allocated *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return homedir;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *pa_get_binary_name_malloc(void) {
|
||||||
|
char *t;
|
||||||
|
size_t allocated = 128;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
t = pa_xmalloc(allocated);
|
||||||
|
|
||||||
|
if (!pa_get_binary_name(t, allocated)) {
|
||||||
|
pa_xfree(t);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strlen(t) < allocated - 1)
|
||||||
|
break;
|
||||||
|
|
||||||
|
pa_xfree(t);
|
||||||
|
allocated *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
static char* make_random_dir(mode_t m) {
|
static char* make_random_dir(mode_t m) {
|
||||||
static const char table[] =
|
static const char table[] =
|
||||||
"abcdefghijklmnopqrstuvwxyz"
|
"abcdefghijklmnopqrstuvwxyz"
|
||||||
|
|
@ -1634,14 +1684,15 @@ FILE *pa_open_config_file(const char *global, const char *local, const char *env
|
||||||
if (local) {
|
if (local) {
|
||||||
const char *e;
|
const char *e;
|
||||||
char *lfn;
|
char *lfn;
|
||||||
char h[PATH_MAX];
|
char *h;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
|
||||||
if ((e = getenv("PULSE_CONFIG_PATH")))
|
if ((e = getenv("PULSE_CONFIG_PATH")))
|
||||||
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", e, local);
|
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", e, local);
|
||||||
else if (pa_get_home_dir(h, sizeof(h)))
|
else if ((h = pa_get_home_dir_malloc())) {
|
||||||
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse" PA_PATH_SEP "%s", h, local);
|
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse" PA_PATH_SEP "%s", h, local);
|
||||||
else
|
pa_xfree(h);
|
||||||
|
} else
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
#ifdef OS_IS_WIN32
|
#ifdef OS_IS_WIN32
|
||||||
|
|
@ -1721,13 +1772,14 @@ char *pa_find_config_file(const char *global, const char *local, const char *env
|
||||||
if (local) {
|
if (local) {
|
||||||
const char *e;
|
const char *e;
|
||||||
char *lfn;
|
char *lfn;
|
||||||
char h[PATH_MAX];
|
char *h;
|
||||||
|
|
||||||
if ((e = getenv("PULSE_CONFIG_PATH")))
|
if ((e = getenv("PULSE_CONFIG_PATH")))
|
||||||
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", e, local);
|
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", e, local);
|
||||||
else if (pa_get_home_dir(h, sizeof(h)))
|
else if ((h = pa_get_home_dir_malloc())) {
|
||||||
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse" PA_PATH_SEP "%s", h, local);
|
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse" PA_PATH_SEP "%s", h, local);
|
||||||
else
|
pa_xfree(h);
|
||||||
|
} else
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
#ifdef OS_IS_WIN32
|
#ifdef OS_IS_WIN32
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,8 @@ char* pa_find_config_file(const char *global, const char *local, const char *env
|
||||||
|
|
||||||
char *pa_get_runtime_dir(void);
|
char *pa_get_runtime_dir(void);
|
||||||
char *pa_get_state_dir(void);
|
char *pa_get_state_dir(void);
|
||||||
|
char *pa_get_home_dir_malloc(void);
|
||||||
|
char *pa_get_binary_name_malloc(void);
|
||||||
char *pa_runtime_path(const char *fn);
|
char *pa_runtime_path(const char *fn);
|
||||||
char *pa_state_path(const char *fn, pa_bool_t prepend_machine_id);
|
char *pa_state_path(const char *fn, pa_bool_t prepend_machine_id);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -186,10 +186,12 @@ void pa_init_proplist(pa_proplist *p) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_BINARY)) {
|
if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_BINARY)) {
|
||||||
char t[PATH_MAX];
|
char *t;
|
||||||
if (pa_get_binary_name(t, sizeof(t))) {
|
|
||||||
|
if ((t = pa_get_binary_name_malloc())) {
|
||||||
char *c = pa_utf8_filter(t);
|
char *c = pa_utf8_filter(t);
|
||||||
pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_BINARY, c);
|
pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_BINARY, c);
|
||||||
|
pa_xfree(t);
|
||||||
pa_xfree(c);
|
pa_xfree(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,12 +23,33 @@
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <pulse/util.h>
|
#include <pulse/util.h>
|
||||||
|
#include <pulse/xmalloc.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
char exename[PATH_MAX];
|
char *exename;
|
||||||
|
size_t allocated = 128;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
exename = pa_xmalloc(allocated);
|
||||||
|
|
||||||
|
if (!pa_get_binary_name(exename, allocated)) {
|
||||||
|
printf("failed to read binary name\n");
|
||||||
|
pa_xfree(exename);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strlen(exename) < allocated - 1) {
|
||||||
|
printf("%s\n", exename);
|
||||||
|
pa_xfree(exename);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
pa_xfree(exename);
|
||||||
|
allocated *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
printf("%s\n", pa_get_binary_name(exename, sizeof(exename)));
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@
|
||||||
#include <pulse/pulseaudio.h>
|
#include <pulse/pulseaudio.h>
|
||||||
#include <pulse/gccmacro.h>
|
#include <pulse/gccmacro.h>
|
||||||
#include <pulsecore/llist.h>
|
#include <pulsecore/llist.h>
|
||||||
|
#include <pulsecore/core-util.h>
|
||||||
|
|
||||||
/* On some systems SIOCINQ isn't defined, but FIONREAD is just an alias */
|
/* On some systems SIOCINQ isn't defined, but FIONREAD is just an alias */
|
||||||
#if !defined(SIOCINQ) && defined(FIONREAD)
|
#if !defined(SIOCINQ) && defined(FIONREAD)
|
||||||
|
|
@ -459,15 +460,16 @@ static void reset_params(fd_info *i) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *client_name(char *buf, size_t n) {
|
static const char *client_name(char *buf, size_t n) {
|
||||||
char p[PATH_MAX];
|
char *p;
|
||||||
const char *e;
|
const char *e;
|
||||||
|
|
||||||
if ((e = getenv("PADSP_CLIENT_NAME")))
|
if ((e = getenv("PADSP_CLIENT_NAME")))
|
||||||
return e;
|
return e;
|
||||||
|
|
||||||
if (pa_get_binary_name(p, sizeof(p)))
|
if ((p = pa_get_binary_name_malloc())) {
|
||||||
snprintf(buf, n, "OSS Emulation[%s]", p);
|
snprintf(buf, n, "OSS Emulation[%s]", p);
|
||||||
else
|
pa_xfree(p);
|
||||||
|
} else
|
||||||
snprintf(buf, n, "OSS");
|
snprintf(buf, n, "OSS");
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue