In |NCONF_load|, strdup section names rather than adding refs.

The fact that |value_free| expects to free() value->section is
inconsistent with the behavior of |add_string|, which adds a reference
to an existing string.

Along the way, add a |CONF_VALUE_new| method to simplify things a bit.

Change-Id: I438abc80575394e4d8df62a4fe2ff1050e3ba039
Reviewed-on: https://boringssl-review.googlesource.com/5744
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
Matt Braithwaite 2015-05-21 17:56:38 -07:00 committed by Adam Langley
parent 50485c7c0c
commit c4ef5ff112
3 changed files with 49 additions and 7 deletions

View File

@ -111,6 +111,16 @@ CONF *NCONF_new(void *method) {
return conf;
}
CONF_VALUE *CONF_VALUE_new(void) {
CONF_VALUE *v = OPENSSL_malloc(sizeof(CONF_VALUE));
if (!v) {
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
return NULL;
}
memset(v, 0, sizeof(CONF_VALUE));
return v;
}
static void value_free_contents(CONF_VALUE *value) {
if (value->section) {
OPENSSL_free(value->section);
@ -148,7 +158,7 @@ CONF_VALUE *NCONF_new_section(const CONF *conf, const char *section) {
CONF_VALUE *v = NULL, *old_value;
sk = sk_CONF_VALUE_new_null();
v = OPENSSL_malloc(sizeof(CONF_VALUE));
v = CONF_VALUE_new();
if (sk == NULL || v == NULL) {
goto err;
}
@ -369,11 +379,12 @@ const char *NCONF_get_string(const CONF *conf, const char *section,
return value->value;
}
int add_string(const CONF *conf, CONF_VALUE *section, CONF_VALUE *value) {
static int add_string(const CONF *conf, CONF_VALUE *section,
CONF_VALUE *value) {
STACK_OF(CONF_VALUE) *section_stack = (STACK_OF(CONF_VALUE)*) section->value;
CONF_VALUE *old_value;
value->section = section->section;
value->section = OPENSSL_strdup(section->section);
if (!sk_CONF_VALUE_push(section_stack, value)) {
return 0;
}
@ -635,14 +646,12 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
p++;
*p = '\0';
if (!(v = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) {
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
if (!(v = CONF_VALUE_new())) {
goto err;
}
if (psection == NULL) {
psection = section;
}
v->value = NULL;
v->name = OPENSSL_strdup(pname);
if (v->name == NULL) {
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);

31
crypto/conf/internal.h Normal file
View File

@ -0,0 +1,31 @@
/* Copyright (c) 2015, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#ifndef OPENSSL_HEADER_CRYPTO_CONF_INTERNAL_H
#define OPENSSL_HEADER_CRYPTO_CONF_INTERNAL_H
#if defined(__cplusplus)
extern "C" {
#endif
/* CONF_VALUE_new returns a freshly allocated and zeroed |CONF_VALUE|. */
CONF_VALUE *CONF_VALUE_new(void);
#if defined(__cplusplus)
} /* extern C */
#endif
#endif /* OPENSSL_HEADER_CRYPTO_CONF_INTERNAL_H */

View File

@ -70,6 +70,8 @@
#include <openssl/obj.h>
#include <openssl/x509v3.h>
#include "../conf/internal.h"
static char *strip_spaces(char *name);
static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b);
@ -91,7 +93,7 @@ int X509V3_add_value(const char *name, const char *value,
char *tname = NULL, *tvalue = NULL;
if(name && !(tname = BUF_strdup(name))) goto err;
if(value && !(tvalue = BUF_strdup(value))) goto err;
if(!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) goto err;
if(!(vtmp = CONF_VALUE_new())) goto err;
if(!*extlist && !(*extlist = sk_CONF_VALUE_new_null())) goto err;
vtmp->section = NULL;
vtmp->name = tname;