diff options
author | Niels de Vos <ndevos@redhat.com> | 2016-09-13 09:03:56 +0200 |
---|---|---|
committer | Niels de Vos <ndevos@redhat.com> | 2016-10-11 00:14:00 -0700 |
commit | 85e959052148ec481823d55c8b91cdee36da2b43 (patch) | |
tree | 18c1d1c977912a58a6b9dbfe9ec99325ae262d40 /api/src/glfs-fops.c | |
parent | 2e23c62cc50037c8e61bcd9c04348409e7627181 (diff) |
gfapi: warn when glfs_realpath() returned malloc'd memory
glfs_realpath() may return memory allocated with malloc(). Depending on
the memory allocator that the application uses, calling free() on the
returned string can cause segmentation faults or other problems.
Functions that allocate memory, need to match the free'ing of the same
memory allocator and memory accounting. glibc/malloc and jemalloc/free
do not match together (other allocators could probably trigger these
problems as well).
Applications need to provide a pre-allocated buffer, or in case
glfs_realpath() allocates the memory, glfs_free() should be used to free
it.
Change-Id: I5d721a7425674aa700db8a7a436cbedb95a5927f
BUG: 1370931
Signed-off-by: Niels de Vos <ndevos@redhat.com>
Reviewed-on: http://review.gluster.org/15332
CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
Smoke: Gluster Build System <jenkins@build.gluster.org>
NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
Reviewed-by: Kaleb KEITHLEY <kkeithle@redhat.com>
Diffstat (limited to 'api/src/glfs-fops.c')
-rw-r--r-- | api/src/glfs-fops.c | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/api/src/glfs-fops.c b/api/src/glfs-fops.c index bbbf61dddfc..0a59a8dcf1c 100644 --- a/api/src/glfs-fops.c +++ b/api/src/glfs-fops.c @@ -4071,9 +4071,11 @@ invalid_fs: GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_fchdir, 3.4.0); +static gf_boolean_t warn_realpath = _gf_true; /* log once */ -char * -pub_glfs_realpath (struct glfs *fs, const char *path, char *resolved_path) +static char * +glfs_realpath_common (struct glfs *fs, const char *path, char *resolved_path, + gf_boolean_t warn_deprecated) { int ret = -1; char *retpath = NULL; @@ -4088,8 +4090,20 @@ pub_glfs_realpath (struct glfs *fs, const char *path, char *resolved_path) if (resolved_path) retpath = resolved_path; - else - retpath = allocpath = malloc (PATH_MAX + 1); + else if (warn_deprecated) { + retpath = allocpath = malloc (PATH_MAX + 1); + if (warn_realpath) { + warn_realpath = _gf_false; + gf_log (THIS->name, GF_LOG_WARNING, "this application " + "is compiled against an old version of " + "libgfapi, it should use glfs_free() to " + "release the path returned by " + "glfs_realpath()"); + } + } else { + retpath = allocpath = GF_CALLOC (1, PATH_MAX + 1, + glfs_mt_realpath_t); + } if (!retpath) { ret = -1; @@ -4120,9 +4134,11 @@ out: loc_wipe (&loc); if (ret == -1) { - if (allocpath) - free (allocpath); - retpath = NULL; + if (warn_deprecated && allocpath) + free (allocpath); + else if (allocpath) + GF_FREE (allocpath); + retpath = NULL; } glfs_subvol_done (fs, subvol); @@ -4133,7 +4149,22 @@ invalid_fs: return retpath; } -GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_realpath, 3.4.0); + +char * +pub_glfs_realpath34 (struct glfs *fs, const char *path, char *resolved_path) +{ + return glfs_realpath_common (fs, path, resolved_path, _gf_true); +} + +GFAPI_SYMVER_PUBLIC(glfs_realpath34, glfs_realpath, 3.4.0); + +char * +pub_glfs_realpath (struct glfs *fs, const char *path, char *resolved_path) +{ + return glfs_realpath_common (fs, path, resolved_path, _gf_false); +} + +GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_realpath, 3.7.17); char * |