Skip to content

Commit d6089f2

Browse files
UTIL: remake sss_*replace_space() to inplace version
There were no users of those functions that would need a new copy.
1 parent 66578bc commit d6089f2

10 files changed

+47
-112
lines changed

src/responder/common/cache_req/plugins/cache_req_group_by_filter.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cache_req_group_by_filter_prepare_domain_data(struct cache_req *cr,
3232
struct sss_domain_info *domain)
3333
{
3434
TALLOC_CTX *tmp_ctx;
35-
const char *name;
35+
char *name;
3636
errno_t ret;
3737

3838
if (cr->data->name.name == NULL) {
@@ -52,11 +52,7 @@ cache_req_group_by_filter_prepare_domain_data(struct cache_req *cr,
5252
goto done;
5353
}
5454

55-
name = sss_reverse_replace_space(tmp_ctx, name, cr->rctx->override_space);
56-
if (name == NULL) {
57-
ret = ENOMEM;
58-
goto done;
59-
}
55+
sss_reverse_replace_space_inplace(name, cr->rctx->override_space);
6056

6157
talloc_zfree(data->name.lookup);
6258
data->name.lookup = talloc_steal(data, name);

src/responder/common/cache_req/plugins/cache_req_group_by_name.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cache_req_group_by_name_prepare_domain_data(struct cache_req *cr,
3232
struct sss_domain_info *domain)
3333
{
3434
TALLOC_CTX *tmp_ctx;
35-
const char *name;
35+
char *name;
3636
errno_t ret;
3737

3838
if (cr->data->name.name == NULL) {
@@ -52,11 +52,7 @@ cache_req_group_by_name_prepare_domain_data(struct cache_req *cr,
5252
goto done;
5353
}
5454

55-
name = sss_reverse_replace_space(tmp_ctx, name, cr->rctx->override_space);
56-
if (name == NULL) {
57-
ret = ENOMEM;
58-
goto done;
59-
}
55+
sss_reverse_replace_space_inplace(name, cr->rctx->override_space);
6056

6157
name = sss_create_internal_fqname(tmp_ctx, name, domain->name);
6258
if (name == NULL) {

src/responder/common/cache_req/plugins/cache_req_initgroups_by_name.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cache_req_initgroups_by_name_prepare_domain_data(struct cache_req *cr,
3232
struct sss_domain_info *domain)
3333
{
3434
TALLOC_CTX *tmp_ctx;
35-
const char *name;
35+
char *name;
3636
errno_t ret;
3737

3838
if (cr->data->name.name == NULL) {
@@ -52,11 +52,7 @@ cache_req_initgroups_by_name_prepare_domain_data(struct cache_req *cr,
5252
goto done;
5353
}
5454

55-
name = sss_reverse_replace_space(tmp_ctx, name, cr->rctx->override_space);
56-
if (name == NULL) {
57-
ret = ENOMEM;
58-
goto done;
59-
}
55+
sss_reverse_replace_space_inplace(name, cr->rctx->override_space);
6056

6157
name = sss_create_internal_fqname(tmp_ctx, name, domain->name);
6258
if (name == NULL) {

src/responder/common/cache_req/plugins/cache_req_object_by_name.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ cache_req_object_by_name_prepare_domain_data(struct cache_req *cr,
7676
struct sss_domain_info *domain)
7777
{
7878
TALLOC_CTX *tmp_ctx;
79-
const char *name;
79+
char *name;
8080
errno_t ret;
8181

8282
if (cr->data->name.name == NULL) {
@@ -96,11 +96,7 @@ cache_req_object_by_name_prepare_domain_data(struct cache_req *cr,
9696
goto done;
9797
}
9898

99-
name = sss_reverse_replace_space(tmp_ctx, name, cr->rctx->override_space);
100-
if (name == NULL) {
101-
ret = ENOMEM;
102-
goto done;
103-
}
99+
sss_reverse_replace_space_inplace(name, cr->rctx->override_space);
104100

105101
name = sss_create_internal_fqname(tmp_ctx, name, domain->name);
106102
if (name == NULL) {

src/responder/common/cache_req/plugins/cache_req_user_by_filter.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cache_req_user_by_filter_prepare_domain_data(struct cache_req *cr,
3232
struct sss_domain_info *domain)
3333
{
3434
TALLOC_CTX *tmp_ctx;
35-
const char *name;
35+
char *name;
3636
errno_t ret;
3737

3838
if (cr->data->name.name == NULL) {
@@ -52,11 +52,7 @@ cache_req_user_by_filter_prepare_domain_data(struct cache_req *cr,
5252
goto done;
5353
}
5454

55-
name = sss_reverse_replace_space(tmp_ctx, name, cr->rctx->override_space);
56-
if (name == NULL) {
57-
ret = ENOMEM;
58-
goto done;
59-
}
55+
sss_reverse_replace_space_inplace(name, cr->rctx->override_space);
6056

6157
talloc_zfree(data->name.lookup);
6258
data->name.lookup = talloc_steal(data, name);

src/responder/common/cache_req/plugins/cache_req_user_by_name.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cache_req_user_by_name_prepare_domain_data(struct cache_req *cr,
3232
struct sss_domain_info *domain)
3333
{
3434
TALLOC_CTX *tmp_ctx;
35-
const char *name;
35+
char *name;
3636
errno_t ret;
3737

3838
if (cr->data->name.name == NULL) {
@@ -52,11 +52,7 @@ cache_req_user_by_name_prepare_domain_data(struct cache_req *cr,
5252
goto done;
5353
}
5454

55-
name = sss_reverse_replace_space(tmp_ctx, name, cr->rctx->override_space);
56-
if (name == NULL) {
57-
ret = ENOMEM;
58-
goto done;
59-
}
55+
sss_reverse_replace_space_inplace(name, cr->rctx->override_space);
6056

6157
name = sss_create_internal_fqname(tmp_ctx, name, domain->name);
6258
if (name == NULL) {

src/tests/cmocka/test_string_utils.c

+10-47
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323

2424
void test_replace_whitespaces(void **state)
2525
{
26-
TALLOC_CTX *mem_ctx;
27-
const char *input_str = "Lorem ipsum dolor sit amet";
28-
const char *res;
2926
size_t i;
27+
char *input;
3028

3129
struct {
3230
const char *input;
@@ -51,36 +49,18 @@ void test_replace_whitespaces(void **state)
5149
{ NULL, NULL, '\0' },
5250
};
5351

54-
mem_ctx = talloc_new(NULL);
55-
assert_non_null(mem_ctx);
56-
check_leaks_push(mem_ctx);
57-
58-
res = sss_replace_space(mem_ctx, input_str, '\0');
59-
assert_string_equal(res, input_str);
60-
talloc_zfree(res);
61-
62-
res = sss_replace_space(mem_ctx, input_str, '\0');
63-
assert_string_equal(res, input_str);
64-
talloc_zfree(res);
65-
6652
for (i=0; data_set[i].input != NULL; ++i) {
67-
res = sss_replace_space(mem_ctx, data_set[i].input,
68-
data_set[i].replace_char);
69-
assert_non_null(res);
70-
assert_string_equal(res, data_set[i].output);
71-
talloc_zfree(res);
53+
input = strdup(data_set[i].input);
54+
sss_replace_space_inplace(input, data_set[i].replace_char);
55+
assert_string_equal(input, data_set[i].output);
56+
free(input);
7257
}
73-
74-
assert_true(check_leaks_pop(mem_ctx) == true);
75-
talloc_free(mem_ctx);
7658
}
7759

7860
void test_reverse_replace_whitespaces(void **state)
7961
{
80-
TALLOC_CTX *mem_ctx;
81-
char *input_str = discard_const_p(char, "Lorem ipsum dolor sit amet");
82-
char *res;
8362
size_t i;
63+
char *input;
8464

8565
struct {
8666
const char *input;
@@ -109,29 +89,12 @@ void test_reverse_replace_whitespaces(void **state)
10989
{ NULL, NULL, '\0' },
11090
};
11191

112-
mem_ctx = talloc_new(NULL);
113-
assert_non_null(mem_ctx);
114-
check_leaks_push(mem_ctx);
115-
116-
res = sss_reverse_replace_space(mem_ctx, input_str, '\0');
117-
assert_string_equal(res, input_str);
118-
talloc_free(res);
119-
120-
res = sss_reverse_replace_space(mem_ctx, input_str, '\0');
121-
assert_string_equal(res, input_str);
122-
talloc_free(res);
123-
12492
for (i=0; data_set[i].input != NULL; ++i) {
125-
input_str = discard_const_p(char, data_set[i].input);
126-
res = sss_reverse_replace_space(mem_ctx, input_str,
127-
data_set[i].replace_char);
128-
assert_non_null(res);
129-
assert_string_equal(res, data_set[i].output);
130-
talloc_zfree(res);
93+
input = strdup(data_set[i].input);
94+
sss_reverse_replace_space_inplace(input, data_set[i].replace_char);
95+
assert_string_equal(input, data_set[i].output);
96+
free(input);
13197
}
132-
133-
assert_true(check_leaks_pop(mem_ctx) == true);
134-
talloc_free(mem_ctx);
13598
}
13699

137100
void test_guid_blob_to_string_buf(void **state)

src/util/string_utils.c

+20-18
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,37 @@
2222

2323
#include "util/util.h"
2424

25+
static inline void replace_char_inplace(char *p, char match, char sub)
26+
{
27+
for (; *p != '\0'; ++p) {
28+
if (*p == match) {
29+
*p = sub;
30+
}
31+
}
32+
}
33+
2534
char *sss_replace_char(TALLOC_CTX *mem_ctx,
2635
const char *in,
2736
const char match,
2837
const char sub)
2938
{
30-
char *p;
3139
char *out;
3240

3341
out = talloc_strdup(mem_ctx, in);
3442
if (out == NULL) {
3543
return NULL;
3644
}
3745

38-
for (p = out; *p != '\0'; ++p) {
39-
if (*p == match) {
40-
*p = sub;
41-
}
42-
}
46+
replace_char_inplace(out, match, sub);
4347

4448
return out;
4549
}
4650

47-
char * sss_replace_space(TALLOC_CTX *mem_ctx,
48-
const char *orig_name,
49-
const char subst)
51+
void sss_replace_space_inplace(char *orig_name,
52+
const char subst)
5053
{
5154
if (subst == '\0' || subst == ' ') {
52-
return talloc_strdup(mem_ctx, orig_name);
55+
return;
5356
}
5457

5558
if (strchr(orig_name, subst) != NULL) {
@@ -60,28 +63,27 @@ char * sss_replace_space(TALLOC_CTX *mem_ctx,
6063
"Name [%s] already contains replacement character [%c]. " \
6164
"No replacement will be done.\n",
6265
orig_name, subst);
63-
return talloc_strdup(mem_ctx, orig_name);
66+
return;
6467
}
6568

66-
return sss_replace_char(mem_ctx, orig_name, ' ', subst);
69+
replace_char_inplace(orig_name, ' ', subst);
6770
}
6871

69-
char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx,
70-
const char *orig_name,
71-
const char subst)
72+
void sss_reverse_replace_space_inplace(char *orig_name,
73+
const char subst)
7274
{
7375
if (subst == '\0' || subst == ' ') {
74-
return talloc_strdup(mem_ctx, orig_name);
76+
return;
7577
}
7678

7779
if (strchr(orig_name, subst) != NULL && strchr(orig_name, ' ') != NULL) {
7880
DEBUG(SSSDBG_TRACE_FUNC,
7981
"Input [%s] contains replacement character [%c] and space.\n",
8082
orig_name, subst);
81-
return talloc_strdup(mem_ctx, orig_name);
83+
return;
8284
}
8385

84-
return sss_replace_char(mem_ctx, orig_name, subst, ' ');
86+
replace_char_inplace(orig_name, subst, ' ');
8587
}
8688

8789
errno_t guid_blob_to_string_buf(const uint8_t *blob, char *str_buf,

src/util/usertools.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,7 @@ char *sss_output_name(TALLOC_CTX *mem_ctx,
734734
outname = shortname;
735735
}
736736

737-
outname = sss_replace_space(tmp_ctx, outname, replace_space);
738-
if (outname == NULL) {
739-
DEBUG(SSSDBG_CRIT_FAILURE, "sss_replace_space failed\n");
740-
goto done;
741-
}
737+
sss_replace_space_inplace(outname, replace_space);
742738

743739
outname = talloc_steal(mem_ctx, outname);
744740
done:

src/util/util.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,10 @@ char *sss_replace_char(TALLOC_CTX *mem_ctx,
696696
const char match,
697697
const char sub);
698698

699-
char * sss_replace_space(TALLOC_CTX *mem_ctx,
700-
const char *orig_name,
701-
const char replace_char);
702-
char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx,
703-
const char *orig_name,
704-
const char replace_char);
699+
void sss_replace_space_inplace(char *orig_name,
700+
const char replace_char);
701+
void sss_reverse_replace_space_inplace(char *orig_name,
702+
const char replace_char);
705703

706704
#define GUID_BIN_LENGTH 16
707705
/* 16 2-digit hex values + 4 dashes + terminating 0 */

0 commit comments

Comments
 (0)