Adding option to add a null terminator when loading a file.

- This will take care of a potential issue with default credentials
  where the slice pointer is casted as const char * for APIs that need a
  null terminated string.
pull/1292/head
Julien Boeuf 10 years ago
parent 0afc3addf4
commit 28d75d9349
  1. 4
      src/core/security/google_default_credentials.c
  2. 5
      src/core/security/security_context.c
  3. 7
      src/core/support/file.c
  4. 6
      src/core/support/file.h
  5. 2
      test/core/security/create_jwt.c
  6. 4
      test/core/security/fetch_oauth2.c
  7. 23
      test/core/support/file_test.c

@ -127,7 +127,7 @@ static grpc_credentials *create_jwt_creds_from_path(char *creds_path) {
gpr_slice creds_data; gpr_slice creds_data;
int file_ok = 0; int file_ok = 0;
if (creds_path == NULL) return NULL; if (creds_path == NULL) return NULL;
creds_data = gpr_load_file(creds_path, &file_ok); creds_data = gpr_load_file(creds_path, 1, &file_ok);
gpr_free(creds_path); gpr_free(creds_path);
if (file_ok) { if (file_ok) {
result = grpc_jwt_credentials_create( result = grpc_jwt_credentials_create(
@ -145,7 +145,7 @@ static grpc_credentials *create_refresh_token_creds_from_path(
gpr_slice creds_data; gpr_slice creds_data;
int file_ok = 0; int file_ok = 0;
if (creds_path == NULL) return NULL; if (creds_path == NULL) return NULL;
creds_data = gpr_load_file(creds_path, &file_ok); creds_data = gpr_load_file(creds_path, 1, &file_ok);
gpr_free(creds_path); gpr_free(creds_path);
if (file_ok) { if (file_ok) {
result = grpc_refresh_token_credentials_create( result = grpc_refresh_token_credentials_create(

@ -359,7 +359,6 @@ static int ssl_host_matches_name(const tsi_peer *peer,
peer_name = allocated_name; peer_name = allocated_name;
if (!peer_name) return 0; if (!peer_name) return 0;
} }
r = tsi_ssl_peer_matches_name(peer, peer_name); r = tsi_ssl_peer_matches_name(peer, peer_name);
gpr_free(allocated_name); gpr_free(allocated_name);
return r; return r;
@ -453,13 +452,13 @@ static void init_default_pem_root_certs(void) {
if (default_root_certs_path == NULL) { if (default_root_certs_path == NULL) {
default_pem_root_certs = gpr_empty_slice(); default_pem_root_certs = gpr_empty_slice();
} else { } else {
default_pem_root_certs = gpr_load_file(default_root_certs_path, NULL); default_pem_root_certs = gpr_load_file(default_root_certs_path, 0, NULL);
gpr_free(default_root_certs_path); gpr_free(default_root_certs_path);
} }
/* Fall back to installed certs if needed. */ /* Fall back to installed certs if needed. */
if (GPR_SLICE_IS_EMPTY(default_pem_root_certs)) { if (GPR_SLICE_IS_EMPTY(default_pem_root_certs)) {
default_pem_root_certs = gpr_load_file(installed_roots_path, NULL); default_pem_root_certs = gpr_load_file(installed_roots_path, 0, NULL);
} }
} }

@ -41,7 +41,8 @@
#include "src/core/support/string.h" #include "src/core/support/string.h"
gpr_slice gpr_load_file(const char *filename, int *success) { gpr_slice gpr_load_file(const char *filename, int add_null_terminator,
int *success) {
unsigned char *contents = NULL; unsigned char *contents = NULL;
size_t contents_size = 0; size_t contents_size = 0;
unsigned char buf[4096]; unsigned char buf[4096];
@ -76,6 +77,10 @@ gpr_slice gpr_load_file(const char *filename, int *success) {
} }
} }
if (success != NULL) *success = 1; if (success != NULL) *success = 1;
if (add_null_terminator) {
contents = gpr_realloc(contents, contents_size + 1);
contents[contents_size++] = 0;
}
result = gpr_slice_new(contents, contents_size, gpr_free); result = gpr_slice_new(contents, contents_size, gpr_free);
end: end:

@ -44,9 +44,11 @@ extern "C" {
/* File utility functions */ /* File utility functions */
/* Loads the content of a file into a slice. The success parameter, if not NULL, /* Loads the content of a file into a slice. add_null_terminator will add
a NULL terminator if non-zero. The success parameter, if not NULL,
will be set to 1 in case of success and 0 in case of failure. */ will be set to 1 in case of success and 0 in case of failure. */
gpr_slice gpr_load_file(const char *filename, int *success); gpr_slice gpr_load_file(const char *filename, int add_null_terminator,
int *success);
/* Creates a temporary file from a prefix. /* Creates a temporary file from a prefix.
If tmp_filename is not NULL, *tmp_filename is assigned the name of the If tmp_filename is not NULL, *tmp_filename is assigned the name of the

@ -48,7 +48,7 @@ void create_jwt(const char *json_key_file_path, const char *service_url,
grpc_auth_json_key key; grpc_auth_json_key key;
int ok = 0; int ok = 0;
char *jwt; char *jwt;
gpr_slice json_key_data = gpr_load_file(json_key_file_path, &ok); gpr_slice json_key_data = gpr_load_file(json_key_file_path, 1, &ok);
if (!ok) { if (!ok) {
fprintf(stderr, "Could not read %s.\n", json_key_file_path); fprintf(stderr, "Could not read %s.\n", json_key_file_path);
exit(1); exit(1);

@ -77,7 +77,7 @@ static void on_oauth2_response(void *user_data, grpc_mdelem **md_elems,
static grpc_credentials *create_service_account_creds( static grpc_credentials *create_service_account_creds(
const char *json_key_file_path, const char *scope) { const char *json_key_file_path, const char *scope) {
int success; int success;
gpr_slice json_key = gpr_load_file(json_key_file_path, &success); gpr_slice json_key = gpr_load_file(json_key_file_path, 1, &success);
if (!success) { if (!success) {
gpr_log(GPR_ERROR, "Could not read file %s.", json_key_file_path); gpr_log(GPR_ERROR, "Could not read file %s.", json_key_file_path);
exit(1); exit(1);
@ -91,7 +91,7 @@ static grpc_credentials *create_refresh_token_creds(
const char *json_refresh_token_file_path) { const char *json_refresh_token_file_path) {
int success; int success;
gpr_slice refresh_token = gpr_slice refresh_token =
gpr_load_file(json_refresh_token_file_path, &success); gpr_load_file(json_refresh_token_file_path, 1, &success);
if (!success) { if (!success) {
gpr_log(GPR_ERROR, "Could not read file %s.", json_refresh_token_file_path); gpr_log(GPR_ERROR, "Could not read file %s.", json_refresh_token_file_path);
exit(1); exit(1);

@ -49,6 +49,7 @@ static const char prefix[] = "file_test";
static void test_load_empty_file(void) { static void test_load_empty_file(void) {
FILE *tmp = NULL; FILE *tmp = NULL;
gpr_slice slice; gpr_slice slice;
gpr_slice slice_with_null_term;
int success; int success;
char *tmp_name; char *tmp_name;
@ -59,13 +60,19 @@ static void test_load_empty_file(void) {
GPR_ASSERT(tmp != NULL); GPR_ASSERT(tmp != NULL);
fclose(tmp); fclose(tmp);
slice = gpr_load_file(tmp_name, &success); slice = gpr_load_file(tmp_name, 0, &success);
GPR_ASSERT(success == 1); GPR_ASSERT(success == 1);
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0); GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0);
slice_with_null_term = gpr_load_file(tmp_name, 1, &success);
GPR_ASSERT(success == 1);
GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == 1);
GPR_ASSERT(GPR_SLICE_START_PTR(slice_with_null_term)[0] == 0);
remove(tmp_name); remove(tmp_name);
gpr_free(tmp_name); gpr_free(tmp_name);
gpr_slice_unref(slice); gpr_slice_unref(slice);
gpr_slice_unref(slice_with_null_term);
} }
static void test_load_failure(void) { static void test_load_failure(void) {
@ -82,7 +89,7 @@ static void test_load_failure(void) {
fclose(tmp); fclose(tmp);
remove(tmp_name); remove(tmp_name);
slice = gpr_load_file(tmp_name, &success); slice = gpr_load_file(tmp_name, 0, &success);
GPR_ASSERT(success == 0); GPR_ASSERT(success == 0);
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0); GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0);
gpr_free(tmp_name); gpr_free(tmp_name);
@ -92,6 +99,7 @@ static void test_load_failure(void) {
static void test_load_small_file(void) { static void test_load_small_file(void) {
FILE *tmp = NULL; FILE *tmp = NULL;
gpr_slice slice; gpr_slice slice;
gpr_slice slice_with_null_term;
int success; int success;
char *tmp_name; char *tmp_name;
const char *blah = "blah"; const char *blah = "blah";
@ -104,14 +112,21 @@ static void test_load_small_file(void) {
GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah)); GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah));
fclose(tmp); fclose(tmp);
slice = gpr_load_file(tmp_name, &success); slice = gpr_load_file(tmp_name, 0, &success);
GPR_ASSERT(success == 1); GPR_ASSERT(success == 1);
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == strlen(blah)); GPR_ASSERT(GPR_SLICE_LENGTH(slice) == strlen(blah));
GPR_ASSERT(!memcmp(GPR_SLICE_START_PTR(slice), blah, strlen(blah))); GPR_ASSERT(!memcmp(GPR_SLICE_START_PTR(slice), blah, strlen(blah)));
slice_with_null_term = gpr_load_file(tmp_name, 1, &success);
GPR_ASSERT(success == 1);
GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == (strlen(blah) + 1));
GPR_ASSERT(strcmp((const char *)GPR_SLICE_START_PTR(slice_with_null_term),
blah) == 0);
remove(tmp_name); remove(tmp_name);
gpr_free(tmp_name); gpr_free(tmp_name);
gpr_slice_unref(slice); gpr_slice_unref(slice);
gpr_slice_unref(slice_with_null_term);
} }
static void test_load_big_file(void) { static void test_load_big_file(void) {
@ -135,7 +150,7 @@ static void test_load_big_file(void) {
GPR_ASSERT(fwrite(buffer, 1, sizeof(buffer), tmp) == sizeof(buffer)); GPR_ASSERT(fwrite(buffer, 1, sizeof(buffer), tmp) == sizeof(buffer));
fclose(tmp); fclose(tmp);
slice = gpr_load_file(tmp_name, &success); slice = gpr_load_file(tmp_name, 0, &success);
GPR_ASSERT(success == 1); GPR_ASSERT(success == 1);
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == sizeof(buffer)); GPR_ASSERT(GPR_SLICE_LENGTH(slice) == sizeof(buffer));
current = GPR_SLICE_START_PTR(slice); current = GPR_SLICE_START_PTR(slice);

Loading…
Cancel
Save