mirror of
git://git.gnupg.org/gnupg.git
synced 2025-01-17 14:07:03 +01:00
common: Let format_text return an error.
* common/stringhelp.c (format_text): Return NULL on error. * common/t-stringhelp.c (test_format_text): Adjust for change. * g10/gpgcompose.c (show_help): Abort on out of core. * g10/tofu.c (ask_about_binding): Abort on format_text error. (show_statistics): Ditto. (show_warning): Ditto. -- For better re-usability function in common/ shot better not use xmalloc functions. Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
bc01d62dc5
commit
00b7767bc6
@ -1443,12 +1443,13 @@ compare_version_strings (const char *my_version, const char *req_version)
|
|||||||
|
|
||||||
|
|
||||||
/* Format a string so that it fits within about TARGET_COLS columns.
|
/* Format a string so that it fits within about TARGET_COLS columns.
|
||||||
TEXT_IN is copied to a new buffer, which is returned.
|
* TEXT_IN is copied to a new buffer, which is returned. Normally,
|
||||||
Normally, target_cols will be 72 and max_cols is 80. */
|
* target_cols will be 72 and max_cols is 80. On error NULL is
|
||||||
|
* returned and ERRNO is set. */
|
||||||
char *
|
char *
|
||||||
format_text (const char *text_in, int target_cols, int max_cols)
|
format_text (const char *text_in, int target_cols, int max_cols)
|
||||||
{
|
{
|
||||||
const int do_debug = 0;
|
/* const int do_debug = 0; */
|
||||||
|
|
||||||
/* The character under consideration. */
|
/* The character under consideration. */
|
||||||
char *p;
|
char *p;
|
||||||
@ -1460,7 +1461,9 @@ format_text (const char *text_in, int target_cols, int max_cols)
|
|||||||
int copied_last_space = 0;
|
int copied_last_space = 0;
|
||||||
char *text;
|
char *text;
|
||||||
|
|
||||||
text = xstrdup (text_in);
|
text = xtrystrdup (text_in);
|
||||||
|
if (!text)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
p = line = text;
|
p = line = text;
|
||||||
while (1)
|
while (1)
|
||||||
@ -1514,9 +1517,9 @@ format_text (const char *text_in, int target_cols, int max_cols)
|
|||||||
cols_with_left_space = last_space_cols;
|
cols_with_left_space = last_space_cols;
|
||||||
cols_with_right_space = cols;
|
cols_with_right_space = cols;
|
||||||
|
|
||||||
if (do_debug)
|
/* if (do_debug) */
|
||||||
log_debug ("Breaking: '%.*s'\n",
|
/* log_debug ("Breaking: '%.*s'\n", */
|
||||||
(int) ((uintptr_t) p - (uintptr_t) line), line);
|
/* (int) ((uintptr_t) p - (uintptr_t) line), line); */
|
||||||
|
|
||||||
/* The number of columns away from TARGET_COLS. We prefer
|
/* The number of columns away from TARGET_COLS. We prefer
|
||||||
to underflow than to overflow. */
|
to underflow than to overflow. */
|
||||||
@ -1528,21 +1531,22 @@ format_text (const char *text_in, int target_cols, int max_cols)
|
|||||||
max_cols. */
|
max_cols. */
|
||||||
right_penalty += 4 * (cols_with_right_space - max_cols);
|
right_penalty += 4 * (cols_with_right_space - max_cols);
|
||||||
|
|
||||||
if (do_debug)
|
/* if (do_debug) */
|
||||||
log_debug ("Left space => %d cols (penalty: %d); right space => %d cols (penalty: %d)\n",
|
/* log_debug ("Left space => %d cols (penalty: %d); " */
|
||||||
cols_with_left_space, left_penalty,
|
/* "right space => %d cols (penalty: %d)\n", */
|
||||||
cols_with_right_space, right_penalty);
|
/* cols_with_left_space, left_penalty, */
|
||||||
|
/* cols_with_right_space, right_penalty); */
|
||||||
if (last_space_cols && left_penalty <= right_penalty)
|
if (last_space_cols && left_penalty <= right_penalty)
|
||||||
/* Prefer the left space. */
|
|
||||||
{
|
{
|
||||||
if (do_debug)
|
/* Prefer the left space. */
|
||||||
log_debug ("Breaking at left space.\n");
|
/* if (do_debug) */
|
||||||
|
/* log_debug ("Breaking at left space.\n"); */
|
||||||
p = last_space;
|
p = last_space;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (do_debug)
|
/* if (do_debug) */
|
||||||
log_debug ("Breaking at right space.\n");
|
/* log_debug ("Breaking at right space.\n"); */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! *p)
|
if (! *p)
|
||||||
|
@ -886,6 +886,11 @@ test_format_text (void)
|
|||||||
struct test *test = &tests[i];
|
struct test *test = &tests[i];
|
||||||
char *result =
|
char *result =
|
||||||
format_text (test->input, test->target_cols, test->max_cols);
|
format_text (test->input, test->target_cols, test->max_cols);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
fail (1);
|
||||||
|
exit (2);
|
||||||
|
}
|
||||||
if (strcmp (result, test->expected) != 0)
|
if (strcmp (result, test->expected) != 0)
|
||||||
{
|
{
|
||||||
printf ("%s: Test #%d failed.\nExpected: '%s'\nResult: '%s'\n",
|
printf ("%s: Test #%d failed.\nExpected: '%s'\nResult: '%s'\n",
|
||||||
|
@ -306,6 +306,8 @@ show_help (struct option options[])
|
|||||||
if (! option)
|
if (! option)
|
||||||
space = 72;
|
space = 72;
|
||||||
formatted = format_text (tmp, space, space + 4);
|
formatted = format_text (tmp, space, space + 4);
|
||||||
|
if (!format_text)
|
||||||
|
abort ();
|
||||||
|
|
||||||
if (tmp != help)
|
if (tmp != help)
|
||||||
xfree (tmp);
|
xfree (tmp);
|
||||||
|
14
g10/tofu.c
14
g10/tofu.c
@ -1305,7 +1305,7 @@ signature_stats_collect_cb (void *cookie, int argc, char **argv,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Format the first part of a conflict message and return that as a
|
/* Format the first part of a conflict message and return that as a
|
||||||
* malloced string. */
|
* malloced string. Returns NULL on error. */
|
||||||
static char *
|
static char *
|
||||||
format_conflict_msg_part1 (int policy, strlist_t conflict_set,
|
format_conflict_msg_part1 (int policy, strlist_t conflict_set,
|
||||||
const char *email)
|
const char *email)
|
||||||
@ -1586,6 +1586,10 @@ ask_about_binding (ctrl_t ctrl,
|
|||||||
|
|
||||||
{
|
{
|
||||||
char *text = format_conflict_msg_part1 (*policy, conflict_set, email);
|
char *text = format_conflict_msg_part1 (*policy, conflict_set, email);
|
||||||
|
if (!text) /* FIXME: Return the error all the way up. */
|
||||||
|
log_fatal ("format failed: %s\n",
|
||||||
|
gpg_strerror (gpg_error_from_syserror()));
|
||||||
|
|
||||||
es_fputs (text, fp);
|
es_fputs (text, fp);
|
||||||
es_fputc ('\n', fp);
|
es_fputc ('\n', fp);
|
||||||
xfree (text);
|
xfree (text);
|
||||||
@ -1927,7 +1931,7 @@ ask_about_binding (ctrl_t ctrl,
|
|||||||
"call the person to make sure this new key is legitimate.";
|
"call the person to make sure this new key is legitimate.";
|
||||||
}
|
}
|
||||||
textbuf = format_text (text, 72, 80);
|
textbuf = format_text (text, 72, 80);
|
||||||
es_fprintf (fp, "\n%s\n", textbuf);
|
es_fprintf (fp, "\n%s\n", textbuf? textbuf : "[OUT OF CORE!]");
|
||||||
xfree (textbuf);
|
xfree (textbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3191,6 +3195,9 @@ show_statistics (tofu_dbs_t dbs,
|
|||||||
if (es_fclose_snatch (fp, (void **) &tmpmsg, NULL))
|
if (es_fclose_snatch (fp, (void **) &tmpmsg, NULL))
|
||||||
log_fatal ("error snatching memory stream\n");
|
log_fatal ("error snatching memory stream\n");
|
||||||
msg = format_text (tmpmsg, 72, 80);
|
msg = format_text (tmpmsg, 72, 80);
|
||||||
|
if (!msg) /* FIXME: Return the error all the way up. */
|
||||||
|
log_fatal ("format failed: %s\n",
|
||||||
|
gpg_strerror (gpg_error_from_syserror()));
|
||||||
es_free (tmpmsg);
|
es_free (tmpmsg);
|
||||||
|
|
||||||
/* Print a status line but suppress the trailing LF.
|
/* Print a status line but suppress the trailing LF.
|
||||||
@ -3266,6 +3273,9 @@ show_warning (const char *fingerprint, strlist_t user_id_list)
|
|||||||
set_policy_command);
|
set_policy_command);
|
||||||
|
|
||||||
text = format_text (tmpmsg, 72, 80);
|
text = format_text (tmpmsg, 72, 80);
|
||||||
|
if (!text) /* FIXME: Return the error all the way up. */
|
||||||
|
log_fatal ("format failed: %s\n",
|
||||||
|
gpg_strerror (gpg_error_from_syserror()));
|
||||||
xfree (tmpmsg);
|
xfree (tmpmsg);
|
||||||
log_string (GPGRT_LOG_INFO, text);
|
log_string (GPGRT_LOG_INFO, text);
|
||||||
xfree (text);
|
xfree (text);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user