Coverage Report

Created: 2023-06-07 06:46

/src/sudo/lib/util/key_val.c
Line
Count
Source
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 *
38
sudo_new_key_val_v1(const char *key, const char *val)
39
167k
{
40
167k
    size_t key_len = strlen(key);
41
167k
    size_t val_len = strlen(val);
42
167k
    char *cp, *str;
43
167k
    debug_decl(sudo_new_key_val, SUDO_DEBUG_UTIL);
44
45
167k
    cp = str = malloc(key_len + 1 + val_len + 1);
46
167k
    if (cp != NULL) {
47
167k
  memcpy(cp, key, key_len);
48
167k
  cp += key_len;
49
167k
  *cp++ = '=';
50
167k
  memcpy(cp, val, val_len);
51
167k
  cp += val_len;
52
167k
  *cp = '\0';
53
167k
    }
54
55
167k
    debug_return_str(str);
56
167k
}