1    	/*
2    	 * Copyright 2004-2026 the Pacemaker project contributors
3    	 *
4    	 * The version control history for this file may have further details.
5    	 *
6    	 * This source code is licensed under the GNU Lesser General Public License
7    	 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8    	 */
9    	
10   	#include <crm_internal.h>
11   	
12   	#include <regex.h>
13   	#include <stdarg.h>     // va_list, etc.
14   	#include <stdbool.h>
15   	#include <stdio.h>
16   	#include <string.h>
17   	#include <stdlib.h>
18   	#include <ctype.h>
19   	#include <float.h>  // DBL_MIN
20   	#include <limits.h>
21   	#include <sys/types.h>
22   	
23   	#include <bzlib.h>
24   	#include <glib.h>           // gboolean, gchar, g_*, ABS, G*, MIN
25   	
26   	/*!
27   	 * \internal
28   	 * \brief Scan a long long integer from a string
29   	 *
30   	 * \param[in]  text           String to scan
31   	 * \param[out] result         If not NULL, where to store scanned value
32   	 * \param[in]  default_value  Value to use if text is NULL or invalid
33   	 * \param[out] end_text       If not NULL, where to store pointer to first
34   	 *                            non-integer character
35   	 *
36   	 * \return Standard Pacemaker return code (\c pcmk_rc_ok on success,
37   	 *         \c pcmk_rc_bad_input on failed string conversion due to invalid
38   	 *         input, or \c ERANGE if outside long long range)
39   	 * \note Sets \c errno on error
40   	 */
41   	static int
42   	scan_ll(const char *text, long long *result, long long default_value,
43   	        char **end_text)
44   	{
45   	    long long local_result = default_value;
46   	    char *local_end_text = NULL;
47   	    int rc = pcmk_rc_ok;
48   	
49   	    errno = 0;
50   	    if (text != NULL) {
51   	        local_result = strtoll(text, &local_end_text, 10);
52   	        if (errno == ERANGE) {
53   	            rc = errno;
54   	            pcmk__debug("Integer parsed from '%s' was clipped to %lld", text,
55   	                        local_result);
56   	
57   	        } else if (local_end_text == text) {
58   	            rc = pcmk_rc_bad_input;
59   	            local_result = default_value;
60   	            pcmk__debug("Could not parse integer from '%s' (using %lld "
61   	                        "instead): No digits found",
62   	                        text, default_value);
63   	
64   	        } else if (errno != 0) {
65   	            rc = errno;
66   	            local_result = default_value;
67   	            pcmk__debug("Could not parse integer from '%s' (using %lld "
68   	                        "instead): %s",
69   	                        text, default_value, pcmk_rc_str(rc));
70   	        }
71   	
72   	        if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) {
73   	            pcmk__debug("Characters left over after parsing '%s': '%s'",
74   	                        text, local_end_text);
75   	        }
76   	        errno = rc;
77   	    }
78   	    if (end_text != NULL) {
79   	        *end_text = local_end_text;
80   	    }
81   	    if (result != NULL) {
82   	        *result = local_result;
83   	    }
84   	    return rc;
85   	}
86   	
87   	/*!
88   	 * \internal
89   	 * \brief Scan a long long integer value from a string
90   	 *
91   	 * \param[in]  text           The string to scan (may be NULL)
92   	 * \param[out] result         Where to store result (or NULL to ignore)
93   	 * \param[in]  default_value  Value to use if text is NULL or invalid
94   	 *
95   	 * \return Standard Pacemaker return code
96   	 */
97   	int
98   	pcmk__scan_ll(const char *text, long long *result, long long default_value)
99   	{
100  	    long long local_result = default_value;
101  	    int rc = scan_ll(text, &local_result, default_value, NULL);
102  	
103  	    if (result != NULL) {
104  	        *result = local_result;
105  	    }
106  	    return rc;
107  	}
108  	
109  	/*!
110  	 * \internal
111  	 * \brief Scan an integer value from a string, constrained to a minimum
112  	 *
113  	 * \param[in]  text           The string to scan (may be NULL)
114  	 * \param[out] result         Where to store result (or NULL to ignore)
115  	 * \param[in]  minimum        Value to use as default and minimum
116  	 *
117  	 * \return Standard Pacemaker return code
118  	 * \note If the value is larger than the maximum integer, EOVERFLOW will be
119  	 *       returned and \p result will be set to the maximum integer.
120  	 */
121  	int
122  	pcmk__scan_min_int(const char *text, int *result, int minimum)
123  	{
124  	    int rc;
125  	    long long result_ll;
126  	
127  	    rc = pcmk__scan_ll(text, &result_ll, (long long) minimum);
128  	
129  	    if (result_ll < (long long) minimum) {
130  	        pcmk__warn("Clipped '%s' to minimum acceptable value %d", text,
131  	                   minimum);
132  	        result_ll = (long long) minimum;
133  	
134  	    } else if (result_ll > INT_MAX) {
135  	        pcmk__warn("Clipped '%s' to maximum integer %d", text, INT_MAX);
136  	        result_ll = (long long) INT_MAX;
137  	        rc = EOVERFLOW;
138  	    }
139  	
140  	    if (result != NULL) {
141  	        *result = (int) result_ll;
142  	    }
143  	    return rc;
144  	}
145  	
146  	/*!
147  	 * \internal
148  	 * \brief Scan a TCP port number from a string
149  	 *
150  	 * \param[in]  text  The string to scan
151  	 * \param[out] port  Where to store result (or NULL to ignore)
152  	 *
153  	 * \return Standard Pacemaker return code
154  	 * \note \p port will be -1 if \p text is NULL or invalid
155  	 */
156  	int
157  	pcmk__scan_port(const char *text, int *port)
158  	{
159  	    long long port_ll;
160  	    int rc = pcmk__scan_ll(text, &port_ll, -1LL);
161  	
162  	    if (rc != pcmk_rc_ok) {
163  	        pcmk__warn("'%s' is not a valid port: %s", text, pcmk_rc_str(rc));
164  	
165  	    } else if ((text != NULL) // wasn't default or invalid
166  	        && ((port_ll < 0LL) || (port_ll > 65535LL))) {
167  	        pcmk__warn("Ignoring port specification '%s' not in valid range "
168  	                   "(0-65535)",
169  	                   text);
170  	        rc = (port_ll < 0LL)? pcmk_rc_before_range : pcmk_rc_after_range;
171  	        port_ll = -1LL;
172  	    }
173  	    if (port != NULL) {
174  	        *port = (int) port_ll;
175  	    }
176  	    return rc;
177  	}
178  	
179  	/*!
180  	 * \internal
181  	 * \brief Scan a double-precision floating-point value from a string
182  	 *
183  	 * \param[in]      text         The string to parse
184  	 * \param[out]     result       Parsed value on success, or
185  	 *                              \c PCMK__PARSE_DBL_DEFAULT on error
186  	 * \param[in]      default_text Default string to parse if \p text is
187  	 *                              \c NULL
188  	 * \param[out]     end_text     If not \c NULL, where to store a pointer
189  	 *                              to the position immediately after the
190  	 *                              value
191  	 *
192  	 * \return Standard Pacemaker return code (\c pcmk_rc_ok on success,
193  	 *         \c EINVAL on failed string conversion due to invalid input,
194  	 *         \c EOVERFLOW on arithmetic overflow, \c pcmk_rc_underflow
195  	 *         on arithmetic underflow, or \c errno from \c strtod() on
196  	 *         other parse errors)
197  	 */
198  	int
199  	pcmk__scan_double(const char *text, double *result, const char *default_text,
200  	                  char **end_text)
201  	{
202  	    int rc = pcmk_rc_ok;
203  	    char *local_end_text = NULL;
204  	
205  	    pcmk__assert(result != NULL);
206  	    *result = PCMK__PARSE_DBL_DEFAULT;
207  	
208  	    text = (text != NULL) ? text : default_text;
209  	
210  	    if (text == NULL) {
211  	        rc = EINVAL;
212  	        pcmk__debug("No text and no default conversion value supplied");
213  	
214  	    } else {
215  	        errno = 0;
216  	        *result = strtod(text, &local_end_text);
217  	
218  	        if (errno == ERANGE) {
219  	            /*
220  	             * Overflow: strtod() returns +/- HUGE_VAL and sets errno to
221  	             *           ERANGE
222  	             *
223  	             * Underflow: strtod() returns "a value whose magnitude is
224  	             *            no greater than the smallest normalized
225  	             *            positive" double. Whether ERANGE is set is
226  	             *            implementation-defined.
227  	             */
228  	            const char *over_under;
229  	
230  	            if (ABS(*result) > DBL_MIN) {
231  	                rc = EOVERFLOW;
232  	                over_under = "over";
233  	            } else {
234  	                rc = pcmk_rc_underflow;
235  	                over_under = "under";
236  	            }
237  	
238  	            pcmk__debug("Floating-point value parsed from '%s' would %sflow "
239  	                        "(using %g instead)",
240  	                        text, over_under, *result);
241  	
242  	        } else if (errno != 0) {
243  	            rc = errno;
244  	            // strtod() set *result = 0 on parse failure
245  	            *result = PCMK__PARSE_DBL_DEFAULT;
246  	
247  	            pcmk__debug("Could not parse floating-point value from '%s' (using "
248  	                        "%.1f instead): %s",
249  	                        text, PCMK__PARSE_DBL_DEFAULT, pcmk_rc_str(rc));
250  	
251  	        } else if (local_end_text == text) {
252  	            // errno == 0, but nothing was parsed
253  	            rc = EINVAL;
254  	            *result = PCMK__PARSE_DBL_DEFAULT;
255  	
256  	            pcmk__debug("Could not parse floating-point value from '%s' (using "
257  	                        "%.1f instead): No digits found",
258  	                        text, PCMK__PARSE_DBL_DEFAULT);
259  	
260  	        } else if (ABS(*result) <= DBL_MIN) {
261  	            /*
262  	             * errno == 0 and text was parsed, but value might have
263  	             * underflowed.
264  	             *
265  	             * ERANGE might not be set for underflow. Check magnitude
266  	             * of *result, but also make sure the input number is not
267  	             * actually zero (0 <= DBL_MIN is not underflow).
268  	             *
269  	             * This check must come last. A parse failure in strtod()
270  	             * also sets *result == 0, so a parse failure would match
271  	             * this test condition prematurely.
272  	             */
273  	            for (const char *p = text; p != local_end_text; p++) {
274  	                if (strchr("0.eE", *p) == NULL) {
275  	                    rc = pcmk_rc_underflow;
276  	                    pcmk__debug("Floating-point value parsed from '%s' would "
277  	                                "underflow (using %g instead)", text, *result);
278  	                    break;
279  	                }
280  	            }
281  	
282  	        } else {
283  	            pcmk__trace("Floating-point value parsed successfully from '%s': "
284  	                        "%g",
285  	                        text, *result);
286  	        }
287  	
288  	        if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) {
289  	            pcmk__debug("Characters left over after parsing '%s': '%s'", text,
290  	                        local_end_text);
291  	        }
292  	    }
293  	
294  	    if (end_text != NULL) {
295  	        *end_text = local_end_text;
296  	    }
297  	
298  	    return rc;
299  	}
300  	
301  	/*!
302  	 * \internal
303  	 * \brief Parse an <tt>unsigned int</tt> from a string stored in a hash table
304  	 *
305  	 * \param[in]     table        Hash table to search
306  	 * \param[in]     key          Hash table key to use to retrieve string
307  	 * \param[in]     default_val  What to use if key has no entry in table
308  	 * \param[out]    result       If not NULL, where to store parsed integer
309  	 *
310  	 * \return Standard Pacemaker return code
311  	 */
312  	int
313  	pcmk__uint_from_hash(GHashTable *table, const char *key,
314  	                     unsigned int default_val, unsigned int *result)
315  	{
316  	    const char *value;
317  	    long long value_ll;
318  	    int rc = pcmk_rc_ok;
319  	
320  	    CRM_CHECK((table != NULL) && (key != NULL), return EINVAL);
321  	
322  	    if (result != NULL) {
323  	        *result = default_val;
324  	    }
325  	
326  	    value = g_hash_table_lookup(table, key);
327  	    if (value == NULL) {
328  	        return pcmk_rc_ok;
329  	    }
330  	
331  	    rc = pcmk__scan_ll(value, &value_ll, 0LL);
332  	    if (rc != pcmk_rc_ok) {
333  	        pcmk__warn("Using default (%u) for %s because '%s' is not a valid "
334  	                   "integer: %s",
335  	                   default_val, key, value, pcmk_rc_str(rc));
336  	        return rc;
337  	    }
338  	
339  	    if ((value_ll < 0) || (value_ll > UINT_MAX)) {
340  	        pcmk__warn("Using default (%u) for %s because '%s' is not in valid "
341  	                   "range",
342  	                   default_val, key, value);
343  	        return ERANGE;
344  	    }
345  	
346  	    if (result != NULL) {
347  	        *result = (unsigned int) value_ll;
348  	    }
349  	    return pcmk_rc_ok;
350  	}
351  	
352  	/*!
353  	 * \brief Parse milliseconds from a Pacemaker interval specification
354  	 *
355  	 * \param[in]  input      Pacemaker time interval specification (a bare number
356  	 *                        of seconds; a number with a unit, optionally with
357  	 *                        whitespace before and/or after the number; or an ISO
358  	 *                        8601 duration) (can be \c NULL)
359  	 * \param[out] result_ms  Where to store milliseconds equivalent of \p input on
360  	 *                        success (limited to the range of an unsigned integer),
361  	 *                        or 0 if \p input is \c NULL or invalid
362  	 *
363  	 * \return Standard Pacemaker return code
364  	 */
365  	int
366  	pcmk_parse_interval_spec(const char *input, unsigned int *result_ms)
367  	{
368  	    long long msec = PCMK__PARSE_INT_DEFAULT;
369  	    int rc = pcmk_rc_ok;
370  	
371  	    if (input == NULL) {
372  	        msec = 0;
373  	        goto done;
374  	    }
375  	
376  	    if (input[0] == 'P') {
377  	        crm_time_t *period_s = pcmk__time_parse_duration(input);
378  	
379  	        if (period_s == NULL) {
380  	            goto done;
381  	        }
382  	
383  	        msec = pcmk__time_get_seconds(period_s);
384  	
385  	        if (msec > 0) {
386  	            msec = MIN(msec, UINT_MAX / 1000) * 1000;
387  	        }
388  	
389  	        free(period_s);
390  	
391  	    } else {
392  	        rc = pcmk__parse_ms(input, &msec);
393  	    }
394  	
395  	    if (msec < 0) {
396  	        pcmk__warn("Using 0 instead of invalid interval specification '%s'",
397  	                   input);
398  	        msec = 0;
399  	
400  	        if (rc == pcmk_rc_ok) {
401  	            // Preserve any error from pcmk__parse_ms()
402  	            rc = EINVAL;
403  	        }
404  	    }
405  	
406  	done:
407  	    if (result_ms != NULL) {
408  	        *result_ms = (msec >= UINT_MAX)? UINT_MAX : (unsigned int) msec;
409  	    }
410  	    return rc;
411  	}
412  	
413  	/*!
414  	 * \internal
415  	 * \brief Create a hash of a string suitable for use with GHashTable
416  	 *
417  	 * \param[in] v  String to hash
418  	 *
419  	 * \return A hash of \p v compatible with g_str_hash() before glib 2.28
420  	 * \note glib changed their hash implementation:
421  	 *
422  	 * https://gitlab.gnome.org/GNOME/glib/commit/354d655ba8a54b754cb5a3efb42767327775696c
423  	 *
424  	 * Note that the new g_str_hash is presumably a *better* hash (it's actually
425  	 * a correct implementation of DJB's hash), but we need to preserve existing
426  	 * behaviour, because the hash key ultimately determines the "sort" order
427  	 * when iterating through GHashTables, which affects allocation of scores to
428  	 * clone instances when iterating through allowed nodes. It (somehow) also
429  	 * appears to have some minor impact on the ordering of a few pseudo_event IDs
430  	 * in the transition graph.
431  	 */
432  	static unsigned int
433  	pcmk__str_hash(const void *v)
434  	{
435  	    const signed char *p;
436  	    guint32 h = 0;
437  	
438  	    for (p = v; *p != '\0'; p++)
439  	        h = (h << 5) - h + *p;
440  	
441  	    return h;
442  	}
443  	
444  	/*!
445  	 * \internal
446  	 * \brief Create a hash table with case-sensitive strings as keys
447  	 *
448  	 * \param[in] key_destroy_func    Function to free a key
449  	 * \param[in] value_destroy_func  Function to free a value
450  	 *
451  	 * \return Newly allocated hash table
452  	 * \note It is the caller's responsibility to free the result, using
453  	 *       g_hash_table_destroy().
454  	 */
455  	GHashTable *
456  	pcmk__strkey_table(GDestroyNotify key_destroy_func,
457  	                   GDestroyNotify value_destroy_func)
458  	{
459  	    return g_hash_table_new_full(pcmk__str_hash, g_str_equal,
460  	                                 key_destroy_func, value_destroy_func);
461  	}
462  	
463  	/*!
464  	 * \internal
465  	 * \brief Insert string copies into a hash table as key and value
466  	 *
467  	 * \param[in,out] table  Hash table to add to
468  	 * \param[in]     name   String to add a copy of as key
469  	 * \param[in]     value  String to add a copy of as value
470  	 *
471  	 * \note This asserts on invalid arguments or memory allocation failure.
472  	 */
473  	void
474  	pcmk__insert_dup(GHashTable *table, const char *name, const char *value)
475  	{
476  	    pcmk__assert((table != NULL) && (name != NULL));
477  	
478  	    g_hash_table_insert(table, pcmk__str_copy(name), pcmk__str_copy(value));
479  	}
480  	
481  	/* used with hash tables where case does not matter */
482  	static gboolean
483  	pcmk__strcase_equal(const void *a, const void *b)
484  	{
485  	    return pcmk__str_eq((const char *)a, (const char *)b, pcmk__str_casei);
486  	}
487  	
488  	static unsigned int
489  	pcmk__strcase_hash(const void *v)
490  	{
491  	    const signed char *p;
492  	    guint32 h = 0;
493  	
494  	    for (p = v; *p != '\0'; p++)
495  	        h = (h << 5) - h + g_ascii_tolower(*p);
496  	
497  	    return h;
498  	}
499  	
500  	/*!
501  	 * \internal
502  	 * \brief Create a hash table with case-insensitive strings as keys
503  	 *
504  	 * \param[in] key_destroy_func    Function to free a key
505  	 * \param[in] value_destroy_func  Function to free a value
506  	 *
507  	 * \return Newly allocated hash table
508  	 * \note It is the caller's responsibility to free the result, using
509  	 *       g_hash_table_destroy().
510  	 */
511  	GHashTable *
512  	pcmk__strikey_table(GDestroyNotify key_destroy_func,
513  	                    GDestroyNotify value_destroy_func)
514  	{
515  	    return g_hash_table_new_full(pcmk__strcase_hash, pcmk__strcase_equal,
516  	                                 key_destroy_func, value_destroy_func);
517  	}
518  	
519  	static void
520  	copy_str_table_entry(void *key, void *value, void *user_data)
521  	{
522  	    if (key && value && user_data) {
523  	        pcmk__insert_dup((GHashTable *) user_data,
524  	                         (const char *) key, (const char *) value);
525  	    }
526  	}
527  	
528  	/*!
529  	 * \internal
530  	 * \brief Copy a hash table that uses dynamically allocated strings
531  	 *
532  	 * \param[in,out] old_table  Hash table to duplicate
533  	 *
534  	 * \return New hash table with copies of everything in \p old_table
535  	 * \note This assumes the hash table uses dynamically allocated strings -- that
536  	 *       is, both the key and value free functions are free().
537  	 */
538  	GHashTable *
539  	pcmk__str_table_dup(GHashTable *old_table)
540  	{
541  	    GHashTable *new_table = NULL;
542  	
543  	    if (old_table) {
544  	        new_table = pcmk__strkey_table(free, free);
545  	        g_hash_table_foreach(old_table, copy_str_table_entry, new_table);
546  	    }
547  	    return new_table;
548  	}
549  	
550  	/*!
551  	 * \internal
552  	 * \brief Add a word to a string list of words
553  	 *
554  	 * \param[in,out] list       Pointer to current string list (may not be \p NULL)
555  	 * \param[in]     init_size  \p list will be initialized to at least this size,
556  	 *                           if it needs initialization (if 0, use GLib's default
557  	 *                           initial string size)
558  	 * \param[in]     word       String to add to \p list (\p list will be
559  	 *                           unchanged if this is \p NULL or the empty string)
560  	 * \param[in]     separator  String to separate words in \p list
561  	 *
562  	 * \note \p word may contain \p separator, though that would be a bad idea if
563  	 *       the string needs to be parsed later.
564  	 */
565  	void
566  	pcmk__add_separated_word(GString **list, size_t init_size, const char *word,
567  	                         const char *separator)
568  	{
569  	    pcmk__assert((list != NULL) && (separator != NULL));
570  	
571  	    if (pcmk__str_empty(word)) {
572  	        return;
573  	    }
574  	
575  	    if (*list == NULL) {
576  	        if (init_size > 0) {
577  	            *list = g_string_sized_new(init_size);
578  	        } else {
579  	            *list = g_string_new(NULL);
580  	        }
581  	    }
582  	
583  	    if ((*list)->len > 0) {
584  	        // Don't add a separator before the first word in the list
585  	        g_string_append(*list, separator);
586  	    }
587  	    g_string_append(*list, word);
588  	}
589  	
590  	/*!
591  	 * \internal
592  	 * \brief Compress data
593  	 *
594  	 * \param[in]  data        Data to compress
595  	 * \param[in]  length      Number of characters of data to compress
596  	 * \param[out] result      Where to store newly allocated compressed result
597  	 * \param[out] result_len  Where to store actual compressed length of result
598  	 *
599  	 * \return Standard Pacemaker return code
600  	 */
601  	int
602  	pcmk__compress(const char *data, size_t length, char **result, size_t *result_len)
603  	{
604  	    size_t max = (length * 1.01) + 601;     // Max size of the compressed result
605  	    unsigned int dest_len = 0;              // Where bz2 should store the actual size
606  	    int rc = pcmk_rc_ok;
607  	    char *compressed = NULL;
608  	    char *uncompressed = NULL;
609  	
610  	    /* Did the max calculation overflow? */
611  	    if (max < length) {
612  	        return EINVAL;
613  	    }
614  	
615  	    /* BZ2_bzBuffToBuffCompress wants unsigned ints, not size_t.  This function
616  	     * takes size_t arguments to simplify checking in its callers.  Make sure
617  	     * we're not passing BZ2_bzBuffToBuffCompress something that's too large.
618  	     */
619  	#if (SIZE_MAX > UINT_MAX)
620  	    if (max > UINT_MAX) {
621  	        return EINVAL;
622  	    }
623  	#endif
624  	
625  	    compressed = pcmk__assert_alloc(max, sizeof(char));
626  	    uncompressed = pcmk__str_copy(data);
627  	
628  	    dest_len = max;
629  	    rc = BZ2_bzBuffToBuffCompress(compressed, &dest_len, uncompressed, length,
630  	                                  PCMK__BZ2_BLOCKS, 0, PCMK__BZ2_WORK);
631  	    *result_len = dest_len;
632  	
633  	    rc = pcmk__bzlib2rc(rc);
634  	
635  	    free(uncompressed);
636  	
637  	    if (rc != pcmk_rc_ok) {
638  	        pcmk__err("Compression of %zu bytes failed: %s " QB_XS " rc=%d", length,
639  	                  pcmk_rc_str(rc), rc);
640  	        free(compressed);
641  	        return rc;
642  	    }
643  	
644  	    pcmk__trace("Compressed %zu bytes into %zu", length, *result_len);
645  	
646  	    *result = compressed;
647  	    return pcmk_rc_ok;
648  	}
649  	
650  	/*!
651  	 * \internal
652  	 * \brief Parse a boolean value from a string
653  	 *
654  	 * Valid input strings (case-insensitive) are as follows:
655  	 * * \c PCMK_VALUE_TRUE, \c "on", \c "yes", \c "y", or \c "1" for \c true
656  	 * * \c PCMK_VALUE_FALSE, \c PCMK_VALUE_OFF, \c "no", \c "n", or \c "0" for
657  	 *   \c false
658  	 *
659  	 * \param[in]  input   Input string
660  	 * \param[out] result  Where to store result (can be \c NULL; unchanged on
661  	 *                     error)
662  	 *
663  	 * \retval Standard Pacemaker return code
664  	 */
665  	int
666  	pcmk__parse_bool(const char *input, bool *result)
667  	{
668  	    bool local_result = false;
669  	
670  	    CRM_CHECK(input != NULL, return EINVAL);
671  	
672  	    if (pcmk__strcase_any_of(input, PCMK_VALUE_TRUE, "on", "yes", "y", "1",
673  	                             NULL)) {
674  	        local_result = true;
675  	
676  	    } else if (pcmk__strcase_any_of(input, PCMK_VALUE_FALSE, PCMK_VALUE_OFF,
677  	                                    "no", "n", "0", NULL)) {
678  	        local_result = false;
679  	
680  	    } else {
681  	        return pcmk_rc_bad_input;
682  	    }
683  	
684  	    if (result != NULL) {
685  	        *result = local_result;
686  	    }
687  	    return pcmk_rc_ok;
688  	}
689  	
690  	/*!
691  	 * \internal
692  	 * \brief Parse a range specification string
693  	 *
694  	 * A valid range specification string can be in any of the following forms,
695  	 * where \c "X", \c "Y", and \c "Z" are nonnegative integers that fit into a
696  	 * <tt>long long</tt> variable:
697  	 * * "X-Y"
698  	 * * "X-"
699  	 * * "-Y"
700  	 * * "Z"
701  	 *
702  	 * In the list above, \c "X" is the start value and \c "Y" is the end value of
703  	 * the range. Either the start value or the end value, but not both, can be
704  	 * empty. \c "Z", a single integer with no \c '-' character, is both the start
705  	 * value and the end value of its range.
706  	 *
707  	 * If the start value or end value is empty, then the parsed result stored in
708  	 * \p *start or \p *end (respectively) is \c PCMK__PARSE_INT_DEFAULT after a
709  	 * successful parse.
710  	 *
711  	 * If the specification string consists of only a single number, then the same
712  	 * value is stored in both \p *start and \p *end on a successful parse.
713  	 *
714  	 * \param[in]  text   String to parse
715  	 * \param[out] start  Where to store start value (can be \c NULL)
716  	 * \param[out] end    Where to store end value (can be \c NULL)
717  	 *
718  	 * \return Standard Pacemaker return code
719  	 *
720  	 * \note The values stored in \p *start and \p *end are undefined if the return
721  	 *       value is not \c pcmk_rc_ok.
722  	 */
723  	int
724  	pcmk__parse_ll_range(const char *text, long long *start, long long *end)
725  	{
726  	    int rc = pcmk_rc_ok;
727  	    long long local_start = 0;
728  	    long long local_end = 0;
729  	    gchar **split = NULL;
730  	    unsigned int length = 0;
731  	    const char *start_s = NULL;
732  	    const char *end_s = NULL;
733  	
734  	    // Do not free
735  	    char *remainder = NULL;
736  	
737  	    if (start == NULL) {
738  	        start = &local_start;
739  	    }
740  	    if (end == NULL) {
741  	        end = &local_end;
742  	    }
743  	    *start = PCMK__PARSE_INT_DEFAULT;
744  	    *end = PCMK__PARSE_INT_DEFAULT;
745  	
746  	    if (pcmk__str_empty(text)) {
747  	        rc = ENODATA;
748  	        goto done;
749  	    }
750  	
751  	    split = g_strsplit(text, "-", 2);
752  	    length = g_strv_length(split);
753  	    start_s = split[0];
754  	    if (length == 2) {
755  	        end_s = split[1];
756  	    }
757  	
758  	    if (pcmk__str_empty(start_s) && pcmk__str_empty(end_s)) {
759  	        rc = pcmk_rc_bad_input;
760  	        goto done;
761  	    }
762  	
763  	    if (!pcmk__str_empty(start_s)) {
764  	        rc = scan_ll(start_s, start, PCMK__PARSE_INT_DEFAULT, &remainder);
765  	        if (rc != pcmk_rc_ok) {
766  	            goto done;
767  	        }
768  	        if (!pcmk__str_empty(remainder)) {
769  	            rc = pcmk_rc_bad_input;
770  	            goto done;
771  	        }
772  	    }
773  	
774  	    if (length == 1) {
775  	        // String contains only a single number, which is both start and end
776  	        *end = *start;
777  	        goto done;
778  	    }
779  	
780  	    if (!pcmk__str_empty(end_s)) {
781  	        rc = scan_ll(end_s, end, PCMK__PARSE_INT_DEFAULT, &remainder);
782  	
783  	        if ((rc == pcmk_rc_ok) && !pcmk__str_empty(remainder)) {
784  	            rc = pcmk_rc_bad_input;
785  	        }
786  	    }
787  	
788  	done:
789  	    g_strfreev(split);
790  	    return rc;
791  	}
792  	
793  	/*!
794  	 * \internal
795  	 * \brief Get multiplier and divisor corresponding to given units string
796  	 *
797  	 * Multiplier and divisor convert from a number of seconds to an equivalent
798  	 * number of the unit described by the units string.
799  	 *
800  	 * \param[in]  units       String describing a unit of time (may be empty,
801  	 *                         \c "s", \c "sec", \c "ms", \c "msec", \c "us",
802  	 *                         \c "usec", \c "m", \c "min", \c "h", or \c "hr")
803  	 * \param[out] multiplier  Number of units in one second, if unit is smaller
804  	 *                         than one second, or 1 otherwise (unchanged on error)
805  	 * \param[out] divisor     Number of seconds in one unit, if unit is larger
806  	 *                         than one second, or 1 otherwise (unchanged on error)
807  	 *
808  	 * \return Standard Pacemaker return code
809  	 */
810  	static int
811  	get_multiplier_divisor(const char *units, long long *multiplier,
812  	                       long long *divisor)
813  	{
814  	    /* @COMPAT Use exact comparisons. Currently, we match too liberally, and the
815  	     * second strncasecmp() in each case is redundant.
816  	     */
817  	    if ((*units == '\0')
818  	        || (strncasecmp(units, "s", 1) == 0)
819  	        || (strncasecmp(units, "sec", 3) == 0)) {
820  	        *multiplier = 1000;
821  	        *divisor = 1;
822  	
823  	    } else if ((strncasecmp(units, "ms", 2) == 0)
824  	               || (strncasecmp(units, "msec", 4) == 0)) {
825  	        *multiplier = 1;
826  	        *divisor = 1;
827  	
828  	    } else if ((strncasecmp(units, "us", 2) == 0)
829  	               || (strncasecmp(units, "usec", 4) == 0)) {
830  	        *multiplier = 1;
831  	        *divisor = 1000;
832  	
833  	    } else if ((strncasecmp(units, "m", 1) == 0)
834  	               || (strncasecmp(units, "min", 3) == 0)) {
835  	        *multiplier = 60 * 1000;
836  	        *divisor = 1;
837  	
838  	    } else if ((strncasecmp(units, "h", 1) == 0)
839  	               || (strncasecmp(units, "hr", 2) == 0)) {
840  	        *multiplier = 60 * 60 * 1000;
841  	        *divisor = 1;
842  	
843  	    } else {
844  	        // Invalid units
845  	        return pcmk_rc_bad_input;
846  	    }
847  	
848  	    return pcmk_rc_ok;
849  	}
850  	
851  	/*!
852  	 * \internal
853  	 * \brief Parse a time and units string into a milliseconds value
854  	 *
855  	 * \param[in]  input   String with a nonnegative number and optional unit
856  	 *                     (optionally with whitespace before and/or after the
857  	 *                     number). If absent, the unit defaults to seconds.
858  	 * \param[out] result  Where to store result in milliseconds (unchanged on error
859  	 *                     except \c ERANGE)
860  	 *
861  	 * \return Standard Pacemaker return code
862  	 */
863  	int
864  	pcmk__parse_ms(const char *input, long long *result)
865  	{
866  	    long long local_result = 0;
867  	    char *units = NULL; // Do not free; will point to part of input
868  	    long long multiplier = 1000;
869  	    long long divisor = 1;
870  	    int rc = pcmk_rc_ok;
871  	
872  	    CRM_CHECK(input != NULL, return EINVAL);
873  	
874  	    rc = scan_ll(input, &local_result, 0, &units);
875  	    if ((rc == pcmk_rc_ok) || (rc == ERANGE)) {
876  	        int units_rc = pcmk_rc_ok;
877  	
878  	        /* If the number is a decimal, scan_ll() reads only the integer part.
879  	         * Skip any remaining digits or decimal characters.
880  	         *
881  	         * @COMPAT Well-formed and malformed decimals are both accepted inputs.
882  	         * For example, "3.14 ms" and "3.1.4 ms" are treated the same as "3ms"
883  	         * and parsed successfully. At a compatibility break, decide if this is
884  	         * still desired.
885  	         */
886  	        for (; isdigit(*units) || (*units == '.'); units++);
887  	
888  	        // Skip any additional whitespace after the number
889  	        for (; isspace(*units); units++);
890  	
891  	        // Validate units and get conversion constants
892  	        units_rc = get_multiplier_divisor(units, &multiplier, &divisor);
893  	        if (units_rc != pcmk_rc_ok) {
894  	            rc = units_rc;
895  	        }
896  	    }
897  	
898  	    if (rc == ERANGE) {
899  	        pcmk__warn("'%s' will be clipped to %lld", input, local_result);
900  	
901  	        /* Continue through rest of body before returning ERANGE
902  	         *
903  	         * @COMPAT Improve handling of overflow. Units won't necessarily be
904  	         * respected right now, for one thing.
905  	         */
906  	
907  	    } else if (rc != pcmk_rc_ok) {
908  	        pcmk__warn("'%s' is not a valid time duration: %s", input,
909  	                   pcmk_rc_str(rc));
910  	        return rc;
911  	    }
912  	
913  	    if (result == NULL) {
914  	        return rc;
915  	    }
916  	
917  	    // Apply units, capping at LLONG_MAX
918  	    if (local_result > (LLONG_MAX / multiplier)) {
919  	        *result = LLONG_MAX;
920  	    } else if (local_result < (LLONG_MIN / multiplier)) {
921  	        *result = LLONG_MIN;
922  	    } else {
923  	        *result = (local_result * multiplier) / divisor;
924  	    }
925  	
926  	    return rc;
927  	}
928  	
929  	/*!
930  	 * \internal
931  	 * \brief Data for \c cmp_str_in_list()
932  	 */
933  	struct str_in_list_data {
934  	    const char *str;
935  	    uint32_t flags;
936  	};
937  	
938  	/*!
939  	 * \internal
940  	 * \brief Call \c pcmk__strcmp() against an element of a \c GList
941  	 *
942  	 * \param[in] a  List element (a string)
943  	 * \param[in] b  String to compare against \p and the flags for comparison (a
944  	 *               (<tt>struct str_in_list_data</tt>)
945  	 *
946  	 * \return A negative integer if \p a comes before \p b->str, a positive integer
947  	 *         if \p a comes after \p b->str, or 0 if \p a is equal to \p b->str
948  	 *         (according to \p b->flags)
949  	 */
950  	static int
951  	cmp_str_in_list(const void *a, const void *b)
952  	{
953  	    const char *element = a;
954  	    const struct str_in_list_data *data = b;
955  	
956  	    return pcmk__strcmp(element, data->str, data->flags);
957  	}
958  	
959  	/*!
960  	 * \internal
961  	 * \brief Find a string in a list of strings
962  	 *
963  	 * \param[in] str    String to search for
964  	 * \param[in] list   List to search
965  	 * \param[in] flags  Group of <tt>enum pcmk__str_flags</tt> to pass to
966  	 *                   \c pcmk__str_eq()
967  	 *
968  	 * \return \c true if \p str is in \p list, or \c false otherwise
969  	 */
970  	bool
971  	pcmk__str_in_list(const char *str, const GList *list, uint32_t flags)
972  	{
973  	    const struct str_in_list_data data = {
974  	        .str = str,
975  	        .flags = flags,
976  	    };
977  	
978  	    return (g_list_find_custom((GList *) list, &data, cmp_str_in_list) != NULL);
979  	}
980  	
981  	/*!
982  	 * \internal
983  	 * \brief Check whether a string is in an array of <tt>gchar *</tt>
984  	 *
985  	 * \param[in] strv  <tt>NULL</tt>-terminated array of strings to search
986  	 * \param[in] str   String to search for
987  	 *
988  	 * \return \c true if \p str is an element of \p strv, or \c false otherwise
989  	 */
990  	bool
991  	pcmk__g_strv_contains(char **strv, const char *str)
992  	{
993  	    // @COMPAT Replace with calls to g_strv_contains() when we require glib 2.44
994  	    CRM_CHECK((strv != NULL) && (str != NULL), return false);
995  	
996  	    for (; *strv != NULL; strv++) {
997  	        if (pcmk__str_eq(*strv, str, pcmk__str_none)) {
998  	            return true;
999  	        }
1000 	    }
1001 	
1002 	    return false;
1003 	}
1004 	
1005 	static bool
1006 	str_any_of(const char *s, va_list args, uint32_t flags)
1007 	{
1008 	    if (s == NULL) {
1009 	        return false;
1010 	    }
1011 	
1012 	    while (1) {
1013 	        const char *ele = va_arg(args, const char *);
1014 	
1015 	        if (ele == NULL) {
1016 	            break;
1017 	        } else if (pcmk__str_eq(s, ele, flags)) {
1018 	            return true;
1019 	        }
1020 	    }
1021 	
1022 	    return false;
1023 	}
1024 	
1025 	/*!
1026 	 * \internal
1027 	 * \brief Is a string a member of a list of strings?
1028 	 *
1029 	 * \param[in]  s    String to search for in \p ...
1030 	 * \param[in]  ...  Strings to compare \p s against.  The final string
1031 	 *                  must be NULL.
1032 	 *
1033 	 * \note The comparison is done case-insensitively.  The function name is
1034 	 *       meant to be reminiscent of strcasecmp.
1035 	 *
1036 	 * \return \c true if \p s is in \p ..., or \c false otherwise
1037 	 */
1038 	bool
1039 	pcmk__strcase_any_of(const char *s, ...)
1040 	{
1041 	    va_list ap;
1042 	    bool rc;
1043 	
1044 	    va_start(ap, s);
1045 	    rc = str_any_of(s, ap, pcmk__str_casei);
1046 	    va_end(ap);
1047 	    return rc;
1048 	}
1049 	
1050 	/*!
1051 	 * \internal
1052 	 * \brief Is a string a member of a list of strings?
1053 	 *
1054 	 * \param[in]  s    String to search for in \p ...
1055 	 * \param[in]  ...  Strings to compare \p s against.  The final string
1056 	 *                  must be NULL.
1057 	 *
1058 	 * \note The comparison is done taking case into account.
1059 	 *
1060 	 * \return \c true if \p s is in \p ..., or \c false otherwise
1061 	 */
1062 	bool
1063 	pcmk__str_any_of(const char *s, ...)
1064 	{
1065 	    va_list ap;
1066 	    bool rc;
1067 	
1068 	    va_start(ap, s);
1069 	    rc = str_any_of(s, ap, pcmk__str_none);
1070 	    va_end(ap);
1071 	    return rc;
1072 	}
1073 	
1074 	/*!
1075 	 * \internal
1076 	 * \brief Sort strings, with numeric portions sorted numerically
1077 	 *
1078 	 * Sort two strings case-insensitively like strcasecmp(), but with any numeric
1079 	 * portions of the string sorted numerically. This is particularly useful for
1080 	 * node names (for example, "node10" will sort higher than "node9" but lower
1081 	 * than "remotenode9").
1082 	 *
1083 	 * \param[in] s1  First string to compare (must not be NULL)
1084 	 * \param[in] s2  Second string to compare (must not be NULL)
1085 	 *
1086 	 * \retval -1 \p s1 comes before \p s2
1087 	 * \retval  0 \p s1 and \p s2 are equal
1088 	 * \retval  1 \p s1 comes after \p s2
1089 	 */
1090 	int
1091 	pcmk__numeric_strcasecmp(const char *s1, const char *s2)
1092 	{
1093 	    pcmk__assert((s1 != NULL) && (s2 != NULL));
1094 	
1095 	    while (*s1 && *s2) {
1096 	        if (isdigit(*s1) && isdigit(*s2)) {
1097 	            // If node names contain a number, sort numerically
1098 	
1099 	            char *end1 = NULL;
1100 	            char *end2 = NULL;
1101 	            long num1 = strtol(s1, &end1, 10);
1102 	            long num2 = strtol(s2, &end2, 10);
1103 	
1104 	            // allow ordering e.g. 007 > 7
1105 	            size_t len1 = end1 - s1;
1106 	            size_t len2 = end2 - s2;
1107 	
1108 	            if (num1 < num2) {
1109 	                return -1;
1110 	            } else if (num1 > num2) {
1111 	                return 1;
1112 	            } else if (len1 < len2) {
1113 	                return -1;
1114 	            } else if (len1 > len2) {
1115 	                return 1;
1116 	            }
1117 	            s1 = end1;
1118 	            s2 = end2;
1119 	        } else {
1120 	            // Compare non-digits case-insensitively
1121 	            int lower1 = tolower(*s1);
1122 	            int lower2 = tolower(*s2);
1123 	
1124 	            if (lower1 < lower2) {
1125 	                return -1;
1126 	            } else if (lower1 > lower2) {
1127 	                return 1;
1128 	            }
1129 	            ++s1;
1130 	            ++s2;
1131 	        }
1132 	    }
1133 	    if (!*s1 && *s2) {
1134 	        return -1;
1135 	    } else if (*s1 && !*s2) {
1136 	        return 1;
1137 	    }
1138 	    return 0;
1139 	}
1140 	
1141 	/*!
1142 	 * \internal
1143 	 * \brief Sort strings.
1144 	 *
1145 	 * This is your one-stop function for string comparison. By default, this
1146 	 * function works like \p g_strcmp0. That is, like \p strcmp but a \p NULL
1147 	 * string sorts before a non-<tt>NULL</tt> string.
1148 	 *
1149 	 * The \p pcmk__str_none flag produces the default behavior. Behavior can be
1150 	 * changed with various flags:
1151 	 *
1152 	 * - \p pcmk__str_regex - The second string is a regular expression that the
1153 	 *                        first string will be matched against.
1154 	 * - \p pcmk__str_casei - By default, comparisons are done taking case into
1155 	 *                        account. This flag makes comparisons case-
1156 	 *                        insensitive. This can be combined with
1157 	 *                        \p pcmk__str_regex.
1158 	 * - \p pcmk__str_null_matches - If one string is \p NULL and the other is not,
1159 	 *                               still return \p 0.
1160 	 * - \p pcmk__str_star_matches - If one string is \p "*" and the other is not,
1161 	 *                               still return \p 0.
1162 	 *
1163 	 * \param[in] s1     First string to compare
1164 	 * \param[in] s2     Second string to compare, or a regular expression to
1165 	 *                   match if \p pcmk__str_regex is set
1166 	 * \param[in] flags  A bitfield of \p pcmk__str_flags to modify operation
1167 	 *
1168 	 * \retval  negative \p s1 is \p NULL or comes before \p s2
1169 	 * \retval  0        \p s1 and \p s2 are equal, or \p s1 is found in \p s2 if
1170 	 *                   \c pcmk__str_regex is set
1171 	 * \retval  positive \p s2 is \p NULL or \p s1 comes after \p s2, or \p s2
1172 	 *                   is an invalid regular expression, or \p s1 was not found
1173 	 *                   in \p s2 if \p pcmk__str_regex is set.
1174 	 */
1175 	int
1176 	pcmk__strcmp(const char *s1, const char *s2, uint32_t flags)
1177 	{
1178 	    /* If this flag is set, the second string is a regex. */
1179 	    if (pcmk__is_set(flags, pcmk__str_regex)) {
1180 	        regex_t r_patt;
1181 	        int reg_flags = REG_EXTENDED | REG_NOSUB;
1182 	        int regcomp_rc = 0;
1183 	        int rc = 0;
1184 	
1185 	        if (s1 == NULL || s2 == NULL) {
1186 	            return 1;
1187 	        }
1188 	
1189 	        if (pcmk__is_set(flags, pcmk__str_casei)) {
1190 	            reg_flags |= REG_ICASE;
1191 	        }
1192 	        regcomp_rc = regcomp(&r_patt, s2, reg_flags);
1193 	        if (regcomp_rc != 0) {
1194 	            rc = 1;
1195 	            pcmk__err("Bad regex '%s' for update: %s", s2,
1196 	                      strerror(regcomp_rc));
1197 	        } else {
1198 	            rc = regexec(&r_patt, s1, 0, NULL, 0);
1199 	            regfree(&r_patt);
1200 	            if (rc != 0) {
1201 	                rc = 1;
1202 	            }
1203 	        }
1204 	        return rc;
1205 	    }
1206 	
1207 	    /* If the strings are the same pointer, return 0 immediately. */
1208 	    if (s1 == s2) {
1209 	        return 0;
1210 	    }
1211 	
1212 	    /* If this flag is set, return 0 if either (or both) of the input strings
1213 	     * are NULL.  If neither one is NULL, we need to continue and compare
1214 	     * them normally.
1215 	     */
1216 	    if (pcmk__is_set(flags, pcmk__str_null_matches)) {
1217 	        if (s1 == NULL || s2 == NULL) {
1218 	            return 0;
1219 	        }
1220 	    }
1221 	
1222 	    /* Handle the cases where one is NULL and the str_null_matches flag is not set.
1223 	     * A NULL string always sorts to the beginning.
1224 	     */
1225 	    if (s1 == NULL) {
1226 	        return -1;
1227 	    } else if (s2 == NULL) {
1228 	        return 1;
1229 	    }
1230 	
1231 	    /* If this flag is set, return 0 if either (or both) of the input strings
1232 	     * are "*".  If neither one is, we need to continue and compare them
1233 	     * normally.
1234 	     */
1235 	    if (pcmk__is_set(flags, pcmk__str_star_matches)) {
1236 	        if (strcmp(s1, "*") == 0 || strcmp(s2, "*") == 0) {
1237 	            return 0;
1238 	        }
1239 	    }
1240 	
1241 	    if (pcmk__is_set(flags, pcmk__str_casei)) {
1242 	        return strcasecmp(s1, s2);
1243 	    } else {
1244 	        return strcmp(s1, s2);
1245 	    }
1246 	}
1247 	
1248 	/*!
1249 	 * \internal
1250 	 * \brief Copy a string, asserting on failure
1251 	 *
1252 	 * \param[in] file      File where \p function is located
1253 	 * \param[in] function  Calling function
1254 	 * \param[in] line      Line within \p file
1255 	 * \param[in] str       String to copy (can be \c NULL)
1256 	 *
1257 	 * \return Newly allocated copy of \p str, or \c NULL if \p str is \c NULL
1258 	 *
1259 	 * \note The caller is responsible for freeing the return value using \c free().
1260 	 */
1261 	char *
1262 	pcmk__str_copy_as(const char *file, const char *function, uint32_t line,
1263 	                  const char *str)
1264 	{
(1) Event path: Condition "str != NULL", taking true branch.
1265 	    if (str != NULL) {
(2) Event alloc_fn: Storage is returned from allocation function "strdup".
(3) Event assign: Assigning: "result" = "strdup(str)".
Also see events: [return_alloc]
1266 	        char *result = strdup(str);
1267 	
(4) Event path: Condition "result == NULL", taking false branch.
1268 	        if (result == NULL) {
1269 	            crm_abort(file, function, line, "Out of memory", FALSE, TRUE);
1270 	            crm_exit(CRM_EX_OSERR);
1271 	        }
(5) Event return_alloc: Returning allocated memory "result".
Also see events: [alloc_fn][assign]
1272 	        return result;
1273 	    }
1274 	    return NULL;
1275 	}
1276 	
1277 	/*!
1278 	 * \internal
1279 	 * \brief Update a dynamically allocated string with a new value
1280 	 *
1281 	 * Given a dynamically allocated string and a new value for it, if the string
1282 	 * is different from the new value, free the string and replace it with either a
1283 	 * newly allocated duplicate of the value or NULL as appropriate.
1284 	 *
1285 	 * \param[in,out] str    Pointer to dynamically allocated string
1286 	 * \param[in]     value  New value to duplicate (or NULL)
1287 	 *
1288 	 * \note The caller remains responsibile for freeing \p *str.
1289 	 */
1290 	void
1291 	pcmk__str_update(char **str, const char *value)
1292 	{
1293 	    if ((str != NULL) && !pcmk__str_eq(*str, value, pcmk__str_none)) {
1294 	        free(*str);
1295 	        *str = pcmk__str_copy(value);
1296 	    }
1297 	}
1298 	
1299 	/*!
1300 	 * \internal
1301 	 * \brief Print to an allocated string using \c printf()-style formatting
1302 	 *
1303 	 * This is like \c asprintf() but asserts on any error. The return value cannot
1304 	 * be \c NULL, but it may be an empty string, depending on the format string and
1305 	 * variadic arguments.
1306 	 *
1307 	 * \param[in] format  \c printf() format string
1308 	 * \param[in] ...     \c printf() format arguments
1309 	 *
1310 	 * \return Newly allocated string (guaranteed not to be \c NULL).
1311 	 *
1312 	 * \note The caller is responsible for freeing the return value using \c free().
1313 	 */
1314 	char *
1315 	pcmk__assert_asprintf(const char *format, ...)
1316 	{
1317 	    char *result = NULL;
1318 	    va_list ap;
1319 	
1320 	    va_start(ap, format);
1321 	    pcmk__assert(vasprintf(&result, format, ap) >= 0);
1322 	    va_end(ap);
1323 	
1324 	    return result;
1325 	}
1326 	
1327 	/*!
1328 	 * \internal
1329 	 * \brief Append a list of strings to a destination \p GString
1330 	 *
1331 	 * \param[in,out] buffer  Where to append the strings (must not be \p NULL)
1332 	 * \param[in]     ...     A <tt>NULL</tt>-terminated list of strings
1333 	 *
1334 	 * \note This tends to be more efficient than a single call to
1335 	 *       \p g_string_append_printf().
1336 	 */
1337 	void
1338 	pcmk__g_strcat(GString *buffer, ...)
1339 	{
1340 	    va_list ap;
1341 	
1342 	    pcmk__assert(buffer != NULL);
1343 	    va_start(ap, buffer);
1344 	
1345 	    while (true) {
1346 	        const char *ele = va_arg(ap, const char *);
1347 	
1348 	        if (ele == NULL) {
1349 	            break;
1350 	        }
1351 	        g_string_append(buffer, ele);
1352 	    }
1353 	    va_end(ap);
1354 	}
1355 	
1356 	// Deprecated functions kept only for backward API compatibility
1357 	// LCOV_EXCL_START
1358 	
1359 	#include <crm/common/strings_compat.h>
1360 	
1361 	long long
1362 	crm_get_msec(const char *input)
1363 	{
1364 	    char *units = NULL; // Do not free; will point to part of input
1365 	    long long multiplier = 1000;
1366 	    long long divisor = 1;
1367 	    long long msec = PCMK__PARSE_INT_DEFAULT;
1368 	    int rc = pcmk_rc_ok;
1369 	
1370 	    if (input == NULL) {
1371 	        return PCMK__PARSE_INT_DEFAULT;
1372 	    }
1373 	
1374 	    // Skip initial whitespace
1375 	    while (isspace(*input)) {
1376 	        input++;
1377 	    }
1378 	
1379 	    rc = scan_ll(input, &msec, PCMK__PARSE_INT_DEFAULT, &units);
1380 	
1381 	    if ((rc == ERANGE) && (msec > 0)) {
1382 	        pcmk__warn("'%s' will be clipped to %lld", input, msec);
1383 	
1384 	    } else if ((rc != pcmk_rc_ok) || (msec < 0)) {
1385 	        pcmk__warn("'%s' is not a valid time duration: %s", input,
1386 	                   ((rc == pcmk_rc_ok)? "Negative" : pcmk_rc_str(rc)));
1387 	        return PCMK__PARSE_INT_DEFAULT;
1388 	    }
1389 	
1390 	    /* If the number is a decimal, scan_ll() reads only the integer part. Skip
1391 	     * any remaining digits or decimal characters.
1392 	     *
1393 	     * @COMPAT Well-formed and malformed decimals are both accepted inputs. For
1394 	     * example, "3.14 ms" and "3.1.4 ms" are treated the same as "3ms" and
1395 	     * parsed successfully. At a compatibility break, decide if this is still
1396 	     * desired.
1397 	     */
1398 	    while (isdigit(*units) || (*units == '.')) {
1399 	        units++;
1400 	    }
1401 	
1402 	    // Skip any additional whitespace after the number
1403 	    while (isspace(*units)) {
1404 	        units++;
1405 	    }
1406 	
1407 	    /* @COMPAT Use exact comparisons. Currently, we match too liberally, and the
1408 	     * second strncasecmp() in each case is redundant.
1409 	     */
1410 	    if ((*units == '\0')
1411 	        || (strncasecmp(units, "s", 1) == 0)
1412 	        || (strncasecmp(units, "sec", 3) == 0)) {
1413 	        multiplier = 1000;
1414 	        divisor = 1;
1415 	
1416 	    } else if ((strncasecmp(units, "ms", 2) == 0)
1417 	               || (strncasecmp(units, "msec", 4) == 0)) {
1418 	        multiplier = 1;
1419 	        divisor = 1;
1420 	
1421 	    } else if ((strncasecmp(units, "us", 2) == 0)
1422 	               || (strncasecmp(units, "usec", 4) == 0)) {
1423 	        multiplier = 1;
1424 	        divisor = 1000;
1425 	
1426 	    } else if ((strncasecmp(units, "m", 1) == 0)
1427 	               || (strncasecmp(units, "min", 3) == 0)) {
1428 	        multiplier = 60 * 1000;
1429 	        divisor = 1;
1430 	
1431 	    } else if ((strncasecmp(units, "h", 1) == 0)
1432 	               || (strncasecmp(units, "hr", 2) == 0)) {
1433 	        multiplier = 60 * 60 * 1000;
1434 	        divisor = 1;
1435 	
1436 	    } else {
1437 	        // Invalid units
1438 	        return PCMK__PARSE_INT_DEFAULT;
1439 	    }
1440 	
1441 	    // Apply units, capping at LLONG_MAX
1442 	    if (msec > (LLONG_MAX / multiplier)) {
1443 	        return LLONG_MAX;
1444 	    }
1445 	    return (msec * multiplier) / divisor;
1446 	}
1447 	
1448 	gboolean
1449 	crm_is_true(const char *s)
1450 	{
1451 	    gboolean ret = FALSE;
1452 	
1453 	    return (crm_str_to_boolean(s, &ret) < 0)? FALSE : ret;
1454 	}
1455 	
1456 	int
1457 	crm_str_to_boolean(const char *s, int *ret)
1458 	{
1459 	    if (s == NULL) {
1460 	        return -1;
1461 	    }
1462 	
1463 	    if (pcmk__strcase_any_of(s, PCMK_VALUE_TRUE, "on", "yes", "y", "1", NULL)) {
1464 	        if (ret != NULL) {
1465 	            *ret = TRUE;
1466 	        }
1467 	        return 1;
1468 	    }
1469 	
1470 	    if (pcmk__strcase_any_of(s, PCMK_VALUE_FALSE, PCMK_VALUE_OFF, "no", "n",
1471 	                             "0", NULL)) {
1472 	        if (ret != NULL) {
1473 	            *ret = FALSE;
1474 	        }
1475 	        return 1;
1476 	    }
1477 	    return -1;
1478 	}
1479 	
1480 	char *
1481 	crm_strdup_printf(char const *format, ...)
1482 	{
1483 	    va_list ap;
1484 	    int len = 0;
1485 	    char *string = NULL;
1486 	
1487 	    va_start(ap, format);
1488 	    len = vasprintf(&string, format, ap);
1489 	    pcmk__assert(len > 0);
1490 	    va_end(ap);
1491 	    return string;
1492 	}
1493 	
1494 	// LCOV_EXCL_STOP
1495 	// End deprecated API
1496