From 08ad0c2a9b0962190e04024e1caf9d59eecb47d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Nogueira?= Date: Wed, 12 May 2021 10:19:55 -0300 Subject: [PATCH] lib/package_msg: don't roundtrip data with fmemopen Using fmemopen here wasn't necessary, since memcpy could have been used with way lower overhead. We don't use a dedicated function, because turning a data field into a string is an inefficient operation and shouldn't be encouraged. Also don't initialize data when it's declared, it isn't necessary. --- lib/package_msg.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/lib/package_msg.c b/lib/package_msg.c index 2ba024ec..b7bdfe4b 100644 --- a/lib/package_msg.c +++ b/lib/package_msg.c @@ -34,8 +34,7 @@ int HIDDEN xbps_cb_message(struct xbps_handle *xhp, xbps_dictionary_t pkgd, const char *key) { xbps_data_t msg; - FILE *f = NULL; - const void *data = NULL; + const void *data; const char *pkgver = NULL; size_t len; char *buf = NULL; @@ -52,35 +51,22 @@ xbps_cb_message(struct xbps_handle *xhp, xbps_dictionary_t pkgd, const char *key if (xbps_object_type(msg) != XBPS_TYPE_DATA) goto out; + /* turn data from msg into a string */ data = xbps_data_data_nocopy(msg); len = xbps_data_size(msg); - if ((f = fmemopen(__UNCONST(data), len, "r")) == NULL) { - rv = errno; - xbps_dbg_printf(xhp, "[%s] %s: fmemopen %s\n", __func__, pkgver, strerror(rv)); - goto out; - }; buf = malloc(len+1); assert(buf); - if (fread(buf, len, 1, f) != len) { - if (ferror(f)) { - rv = errno; - xbps_dbg_printf(xhp, "[%s] %s: fread %s\n", __func__, pkgver, strerror(rv)); - goto out; - } - } - /* terminate buffer and notify client to show the post-install message */ + memcpy(buf, data, len); + /* terminate string */ buf[len] = '\0'; + /* notify client to show the post-install message */ if (strcmp(key, "install-msg") == 0) xbps_set_cb_state(xhp, XBPS_STATE_SHOW_INSTALL_MSG, 0, pkgver, "%s", buf); else xbps_set_cb_state(xhp, XBPS_STATE_SHOW_REMOVE_MSG, 0, pkgver, "%s", buf); out: - if (f != NULL) - fclose(f); - if (buf != NULL) - free(buf); - + free(buf); return rv; }