diff options
author | Xavier Leroy <xavier.leroy@inria.fr> | 2017-08-25 18:24:01 +0200 |
---|---|---|
committer | Xavier Leroy <xavier.leroy@inria.fr> | 2017-08-25 18:24:01 +0200 |
commit | 7a315bdb5e66bffdd7cef536373a2e0b753d1b7f (patch) | |
tree | 31c9db7d311540d6f31a6bd0266442a21f8e252b | |
parent | e49e6ce5ee35fb5e7b6647004b0781532d7e03c3 (diff) | |
download | ocaml-7a315bdb5e66bffdd7cef536373a2e0b753d1b7f.tar.gz |
In caml_executable_name(), tighten buffer allocation and clarify code
Follow-up to commit e49e6ce.
-rw-r--r-- | byterun/unix.c | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/byterun/unix.c b/byterun/unix.c index 9d25b33862..3fd72b8eff 100644 --- a/byterun/unix.c +++ b/byterun/unix.c @@ -369,7 +369,7 @@ char * caml_executable_name(void) to determine the size of the buffer. Instead, we guess and adjust. */ namelen = 256; while (1) { - name = caml_stat_alloc(namelen + 1); + name = caml_stat_alloc(namelen); retcode = readlink("/proc/self/exe", name, namelen); if (retcode == -1) { caml_stat_free(name); return NULL; } if (retcode < namelen) break; @@ -377,7 +377,8 @@ char * caml_executable_name(void) if (namelen >= 1024*1024) return NULL; /* avoid runaway and overflow */ namelen *= 2; } - /* readlink() does not zero-terminate its result */ + /* readlink() does not zero-terminate its result. + There is room for a final zero since retcode < namelen. */ name[retcode] = 0; /* Make sure that the contents of /proc/self/exe is a regular file. (Old Linux kernels return an inode number instead.) */ |