diff options
author | NeilBrown <neilb@suse.de> | 2023-05-11 14:22:24 -0400 |
---|---|---|
committer | Steve Dickson <steved@redhat.com> | 2023-05-11 15:04:09 -0400 |
commit | 4f1520f18068519a4cc488b1cea823a4f5ad2de6 (patch) | |
tree | aab536dd9ddd7ffa7c964bbf95a98ad1fb8c5add | |
parent | b17d8c5ad579b0dc0928298db9cc7a9214f1c0c7 (diff) | |
download | nfs-utils-4f1520f18068519a4cc488b1cea823a4f5ad2de6.tar.gz |
fsidd: don't use assert() on expr with side-effect.
assert() is not guaranteed to evaluate its arg. When compiled with
-DNDEBUG, the evaluation is skipped. We don't currently compile with
-DNDEBUG, but relying on that is poor form, particularly as this is
described as "sample code" in the git log.
So introduce assert_safe() and use that when there are side-effects.
Acked-by: Richard Weinberger <richard@nod.at>
Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: Steve Dickson <steved@redhat.com>
-rw-r--r--[l---------] | support/include/version.h | 54 | ||||
-rw-r--r-- | support/reexport/fsidd.c | 28 |
2 files changed, 69 insertions, 13 deletions
diff --git a/support/include/version.h b/support/include/version.h index b7db0bb..d7cf680 120000..100644 --- a/support/include/version.h +++ b/support/include/version.h @@ -1 +1,53 @@ -../../utils/mount/version.h
\ No newline at end of file +/* + * version.h -- get running kernel version + * + * Copyright (C) 2008 Oracle. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 0211-1301 USA + * + */ + +#ifndef _NFS_UTILS_MOUNT_VERSION_H +#define _NFS_UTILS_MOUNT_VERSION_H + +#include <stdio.h> +#include <limits.h> + +#include <sys/utsname.h> + +static inline unsigned int MAKE_VERSION(unsigned int p, unsigned int q, + unsigned int r) +{ + return (65536 * p) + (256 * q) + r; +} + +static inline unsigned int linux_version_code(void) +{ + struct utsname my_utsname; + unsigned int p, q = 0, r = 0; + + /* UINT_MAX as backward compatibility code should not be run */ + if (uname(&my_utsname)) + return UINT_MAX; + + /* UINT_MAX as future versions might not start with an integer */ + if (sscanf(my_utsname.release, "%u.%u.%u", &p, &q, &r) < 1) + return UINT_MAX; + + return MAKE_VERSION(p, q, r); +} + +#endif /* _NFS_UTILS_MOUNT_VERSION_H */ diff --git a/support/reexport/fsidd.c b/support/reexport/fsidd.c index 410b3a3..3fef1ef 100644 --- a/support/reexport/fsidd.c +++ b/support/reexport/fsidd.c @@ -26,6 +26,11 @@ static struct event_base *evbase; static struct reexpdb_backend_plugin *dbbackend = &sqlite_plug_ops; +/* assert_safe() always evalutes it argument, as it might have + * a side-effect. assert() won't if compiled with NDEBUG + */ +#define assert_safe(__sideeffect) (__sideeffect ? 0 : ({assert(0) ; 0;})) + static void client_cb(evutil_socket_t cl, short ev, void *d) { struct event *me = d; @@ -56,12 +61,11 @@ static void client_cb(evutil_socket_t cl, short ev, void *d) if (dbbackend->fsidnum_by_path(req_path, &fsidnum, false, &found)) { if (found) - assert(asprintf(&answer, "+ %u", fsidnum) != -1); + assert_safe(asprintf(&answer, "+ %u", fsidnum) != -1); else - assert(asprintf(&answer, "+ ") != -1); - + assert_safe(asprintf(&answer, "+ ") != -1); } else { - assert(asprintf(&answer, "- %s", "Command failed") != -1); + assert_safe(asprintf(&answer, "- %s", "Command failed") != -1); } (void)send(cl, answer, strlen(answer), 0); @@ -78,13 +82,13 @@ static void client_cb(evutil_socket_t cl, short ev, void *d) if (dbbackend->fsidnum_by_path(req_path, &fsidnum, true, &found)) { if (found) { - assert(asprintf(&answer, "+ %u", fsidnum) != -1); + assert_safe(asprintf(&answer, "+ %u", fsidnum) != -1); } else { - assert(asprintf(&answer, "+ ") != -1); + assert_safe(asprintf(&answer, "+ ") != -1); } } else { - assert(asprintf(&answer, "- %s", "Command failed") != -1); + assert_safe(asprintf(&answer, "- %s", "Command failed") != -1); } (void)send(cl, answer, strlen(answer), 0); @@ -106,15 +110,15 @@ static void client_cb(evutil_socket_t cl, short ev, void *d) } if (bad_input) { - assert(asprintf(&answer, "- %s", "Command failed: Bad input") != -1); + assert_safe(asprintf(&answer, "- %s", "Command failed: Bad input") != -1); } else { if (dbbackend->path_by_fsidnum(fsidnum, &path, &found)) { if (found) - assert(asprintf(&answer, "+ %s", path) != -1); + assert_safe(asprintf(&answer, "+ %s", path) != -1); else - assert(asprintf(&answer, "+ ") != -1); + assert_safe(asprintf(&answer, "+ ") != -1); } else { - assert(asprintf(&answer, "+ ") != -1); + assert_safe(asprintf(&answer, "+ ") != -1); } } @@ -129,7 +133,7 @@ static void client_cb(evutil_socket_t cl, short ev, void *d) } else { char *answer = NULL; - assert(asprintf(&answer, "- bad command") != -1); + assert_safe(asprintf(&answer, "- bad command") != -1); (void)send(cl, answer, strlen(answer), 0); free(answer); |