1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2010-2012, 2014-2015 Todd C. Miller <Todd.Miller@sudo.ws>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22  */
23 
24 #include <config.h>
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "sudo_compat.h"
30 #include "sudo_debug.h"
31 #include "sudo_util.h"
32 
33 /*
34  * Create a new key=value pair and return it.
35  * The caller is responsible for freeing the string.
36  */
37 char *
sudo_new_key_val_v1(const char * key,const char * val)38 sudo_new_key_val_v1(const char *key, const char *val)
39 {
40     size_t key_len = strlen(key);
41     size_t val_len = strlen(val);
42     char *cp, *str;
43     debug_decl(sudo_new_key_val, SUDO_DEBUG_UTIL);
44 
45     cp = str = malloc(key_len + 1 + val_len + 1);
46     if (cp != NULL) {
47 	memcpy(cp, key, key_len);
48 	cp += key_len;
49 	*cp++ = '=';
50 	memcpy(cp, val, val_len);
51 	cp += val_len;
52 	*cp = '\0';
53     }
54 
55     debug_return_str(str);
56 }
57