1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-08 21:18:16 +03:00

lib: Avoid the use of open_memstream in tevent_req_profile_string

Solaris does not have it.

Bug: https://bugzilla.samba.org/show_bug.cgi?id=13629
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
Volker Lendecke 2018-10-07 14:47:26 +02:00 committed by Jeremy Allison
parent f36a538cbd
commit e7d3678ddb
3 changed files with 50 additions and 56 deletions

View File

@ -170,6 +170,7 @@ static bool test_tevent_req_profile2(struct torture_context *tctx,
pid_t pid1, pid2;
enum tevent_req_state state1, state2;
uint64_t err1, err2;
char *printstring;
ssize_t pack_len;
int err;
bool ok;
@ -189,7 +190,12 @@ static bool test_tevent_req_profile2(struct torture_context *tctx,
TALLOC_FREE(req);
TALLOC_FREE(ev);
tevent_req_profile_print(p1, stdout, 0, UINT_MAX);
printstring = tevent_req_profile_string(tctx, p1, 0, UINT_MAX);
torture_assert_not_null(
tctx,
printstring,
"tevent_req_profile_string failed\n");
printf("%s\n", printstring);
pack_len = tevent_req_profile_pack(p1, NULL, 0);
torture_assert(tctx, pack_len>0, "profile_pack failed\n");
@ -212,7 +218,12 @@ static bool test_tevent_req_profile2(struct torture_context *tctx,
"profile_unpack failed\n");
}
tevent_req_profile_print(p2, stdout, 0, UINT_MAX);
printstring = tevent_req_profile_string(tctx, p2, 0, UINT_MAX);
torture_assert_not_null(
tctx,
printstring,
"tevent_req_profile_string failed\n");
printf("%s\n", printstring);
tevent_req_profile_get_name(p1, &str1);
tevent_req_profile_get_name(p2, &str2);

View File

@ -29,10 +29,11 @@
#include "lib/util/time_basic.h"
#include "lib/util/memory.h"
int tevent_req_profile_print(const struct tevent_req_profile *profile,
FILE *fp,
unsigned indent,
unsigned max_indent)
static bool tevent_req_profile_string_internal(
const struct tevent_req_profile *profile,
unsigned indent,
unsigned max_indent,
char **string)
{
struct timeval start, stop, diff;
struct timeval_buf start_buf, stop_buf;
@ -44,7 +45,7 @@ int tevent_req_profile_print(const struct tevent_req_profile *profile,
const char *state_buf = NULL;
uint64_t user_error;
const struct tevent_req_profile *sub = NULL;
int ret;
char *result;
tevent_req_profile_get_name(profile, &req_name);
@ -85,8 +86,8 @@ int tevent_req_profile_print(const struct tevent_req_profile *profile,
break;
}
ret = fprintf(
fp,
result = talloc_asprintf_append_buffer(
*string,
"%*s[%s] %s [%s] %s [%s] [%ju.%.6ju] -> %s (%d %"PRIu64"))\n",
indent,
"",
@ -100,72 +101,58 @@ int tevent_req_profile_print(const struct tevent_req_profile *profile,
state_buf,
(int)state,
user_error);
if (ret < 0) {
return ret;
if (result == NULL) {
return false;
}
*string = result;
indent += 1;
if (indent >= max_indent) {
return ret;
return true;
}
for (sub = tevent_req_profile_get_subprofiles(profile);
sub != NULL;
sub = tevent_req_profile_next(sub)) {
int subret;
bool ret;
subret = tevent_req_profile_print(sub, fp, indent, max_indent);
if (subret < 0) {
return subret;
}
ret += subret;
if (ret < subret) {
/* overflow */
return -1;
ret = tevent_req_profile_string_internal(
sub,
indent,
max_indent,
string);
if (!ret) {
return false;
}
}
return ret;
return true;
}
char *tevent_req_profile_string(const struct tevent_req_profile *profile,
TALLOC_CTX *mem_ctx,
char *tevent_req_profile_string(TALLOC_CTX *mem_ctx,
const struct tevent_req_profile *profile,
unsigned indent,
unsigned max_indent)
{
FILE *fp = NULL;
char *buf = NULL;
size_t buflen = 0;
char *result = NULL;
int ret;
char *result;
bool ret;
fp = open_memstream(&buf, &buflen);
if (fp == NULL) {
result = talloc_strdup(mem_ctx, "");
if (result == NULL) {
return NULL;
}
ret = tevent_req_profile_print(profile, fp, 0, max_indent);
if (ret < 0) {
goto done;
ret = tevent_req_profile_string_internal(
profile,
indent,
max_indent,
&result);
if (!ret) {
TALLOC_FREE(result);
return NULL;
}
ret = fclose(fp);
if (ret != 0) {
goto done;
}
/*
* A FILE* from open_memstream maintains the 0-byte at the end
* beyond the reported length.
*/
result = talloc_memdup(mem_ctx, buf, buflen+1);
done:
SAFE_FREE(buf);
return result;
}

View File

@ -29,12 +29,8 @@
#include "replace.h"
#include <tevent.h>
int tevent_req_profile_print(const struct tevent_req_profile *profile,
FILE *fp,
unsigned indent,
unsigned max_indent);
char *tevent_req_profile_string(const struct tevent_req_profile *profile,
TALLOC_CTX *mem_ctx,
char *tevent_req_profile_string(TALLOC_CTX *mem_ctx,
const struct tevent_req_profile *profile,
unsigned indent,
unsigned max_indent);
ssize_t tevent_req_profile_pack(