Fixed buggy grpc_compression_algorithm_parse

pull/2533/head
David Garcia Quintas 10 years ago
parent c0a09015b1
commit 1c604fd4f5
  1. 9
      include/grpc/compression.h
  2. 2
      src/core/channel/compress_filter.c
  3. 8
      src/core/compression/algorithm.c
  4. 9
      src/core/surface/call.c

@ -34,6 +34,8 @@
#ifndef GRPC_COMPRESSION_H
#define GRPC_COMPRESSION_H
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -58,9 +60,10 @@ typedef enum {
GRPC_COMPRESS_LEVEL_COUNT
} grpc_compression_level;
/** Parses \a name as a grpc_compression_algorithm instance, updating \a
* algorithm. Returns 1 upon success, 0 otherwise. */
int grpc_compression_algorithm_parse(const char *name,
/** Parses the first \a name_length bytes of \a name as a
* grpc_compression_algorithm instance, updating \a algorithm. Returns 1 upon
* success, 0 otherwise. */
int grpc_compression_algorithm_parse(const char *name, size_t name_length,
grpc_compression_algorithm *algorithm);
/** Updates \a name with the encoding name corresponding to a valid \a

@ -100,7 +100,7 @@ static grpc_mdelem* compression_md_filter(void *user_data, grpc_mdelem *md) {
if (md->key == channeld->mdstr_request_compression_algorithm_key) {
const char *md_c_str = grpc_mdstr_as_c_string(md->value);
if (!grpc_compression_algorithm_parse(md_c_str,
if (!grpc_compression_algorithm_parse(md_c_str, strlen(md_c_str),
&calld->compression_algorithm)) {
gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s'. Ignoring.",
md_c_str);

@ -35,17 +35,17 @@
#include <string.h>
#include <grpc/compression.h>
int grpc_compression_algorithm_parse(const char* name,
int grpc_compression_algorithm_parse(const char* name, size_t name_length,
grpc_compression_algorithm *algorithm) {
/* we use strncmp not only because it's safer (even though in this case it
* doesn't matter, given that we are comparing against string literals, but
* because this way we needn't have "name" nil-terminated (useful for slice
* data, for example) */
if (strncmp(name, "none", 4) == 0) {
if (strncmp(name, "none", name_length) == 0) {
*algorithm = GRPC_COMPRESS_NONE;
} else if (strncmp(name, "gzip", 4) == 0) {
} else if (strncmp(name, "gzip", name_length) == 0) {
*algorithm = GRPC_COMPRESS_GZIP;
} else if (strncmp(name, "deflate", 7) == 0) {
} else if (strncmp(name, "deflate", name_length) == 0) {
*algorithm = GRPC_COMPRESS_DEFLATE;
} else {
return 0;

@ -495,7 +495,8 @@ static void set_encodings_accepted_by_peer(grpc_call *call,
for (i = 0; i < accept_encoding_parts.count; i++) {
const gpr_slice* slice = &accept_encoding_parts.slices[i];
if (grpc_compression_algorithm_parse(
(const char *)GPR_SLICE_START_PTR(*slice), &algorithm)) {
(const char *)GPR_SLICE_START_PTR(*slice), GPR_SLICE_LENGTH(*slice),
&algorithm)) {
GPR_BITSET(&call->encodings_accepted_by_peer, algorithm);
} else {
/* TODO(dgq): it'd be nice to have a slice-to-cstr function to easily
@ -1344,10 +1345,12 @@ static gpr_uint32 decode_compression(grpc_mdelem *md) {
grpc_compression_algorithm algorithm;
void *user_data = grpc_mdelem_get_user_data(md, destroy_compression);
if (user_data) {
algorithm = ((grpc_compression_level)(gpr_intptr)user_data) - COMPRESS_OFFSET;
algorithm =
((grpc_compression_level)(gpr_intptr)user_data) - COMPRESS_OFFSET;
} else {
const char *md_c_str = grpc_mdstr_as_c_string(md->value);
if (!grpc_compression_algorithm_parse(md_c_str, &algorithm)) {
if (!grpc_compression_algorithm_parse(md_c_str, strlen(md_c_str),
&algorithm)) {
gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s'", md_c_str);
assert(0);
}

Loading…
Cancel
Save