File pacemaker-libcrmcommon-assert-cant-generate-operation-key.patch of Package pacemaker.14737
commit 47f957c747f76d54b7289d9651aa057efb5be4c7
Author: Ken Gaillot <kgaillot@redhat.com>
Date: Mon Feb 27 14:10:02 2017 -0600
Fix: libcrmcommon: assert if can't generate operation key
This is a refactor of generate_op_key() to use the existing
crm_strdup_printf() function instead of its own allocation,
and use CRM_ASSERT() instead of CRM_CHECK() for argument validation.
This simplifies the code, avoids allocating more memory than needed,
and results in assertion failures instead of returning NULL on errors,
which is preferable because existing callers don't check the
return value for NULL before using it.
diff --git a/lib/common/utils.c b/lib/common/utils.c
index 3e3abd396..dbf84c0c2 100644
--- a/lib/common/utils.c
+++ b/lib/common/utils.c
@@ -629,21 +629,24 @@ crm_get_msec(const char *input)
return msec;
}
+/*!
+ * \brief Generate an operation key
+ *
+ * \param[in] rsc_id ID of resource being operated on
+ * \param[in] op_type Operation name
+ * \param[in] interval Operation interval
+ *
+ * \return Newly allocated memory containing operation key as string
+ *
+ * \note It is the caller's responsibility to free() the result.
+ */
char *
generate_op_key(const char *rsc_id, const char *op_type, int interval)
{
- int len = 35;
- char *op_id = NULL;
-
- CRM_CHECK(rsc_id != NULL, return NULL);
- CRM_CHECK(op_type != NULL, return NULL);
-
- len += strlen(op_type);
- len += strlen(rsc_id);
- op_id = malloc(len);
- CRM_CHECK(op_id != NULL, return NULL);
- sprintf(op_id, "%s_%s_%d", rsc_id, op_type, interval);
- return op_id;
+ CRM_ASSERT(rsc_id != NULL);
+ CRM_ASSERT(op_type != NULL);
+ CRM_ASSERT(interval >= 0);
+ return crm_strdup_printf("%s_%s_%d", rsc_id, op_type, interval);
}
gboolean