libdrgn: add drgn_error_from_string_builder()

And use that instead of exposing drgn_error_create_nodup().
This commit is contained in:
Omar Sandoval 2019-05-14 10:16:01 -07:00
parent b0f10d3b58
commit e21ed988fb
3 changed files with 23 additions and 15 deletions

View File

@ -10,6 +10,7 @@
#include <string.h>
#include "internal.h"
#include "string_builder.h"
LIBDRGN_PUBLIC struct drgn_error drgn_enomem = {
.code = DRGN_ERROR_NO_MEMORY,
@ -26,8 +27,8 @@ struct drgn_error drgn_not_elf = {
.message = "not an ELF file",
};
struct drgn_error *drgn_error_create_nodup(enum drgn_error_code code,
char *message)
static struct drgn_error *drgn_error_create_nodup(enum drgn_error_code code,
char *message)
{
struct drgn_error *err;
@ -102,6 +103,18 @@ LIBDRGN_PUBLIC struct drgn_error *drgn_error_format(enum drgn_error_code code,
return drgn_error_create_nodup(code, message);
}
struct drgn_error *drgn_error_from_string_builder(enum drgn_error_code code,
struct string_builder *sb)
{
char *message;
if (!string_builder_finalize(sb, &message)) {
free(sb->str);
return &drgn_enomem;
}
return drgn_error_create_nodup(code, message);
}
LIBDRGN_PUBLIC void drgn_error_fwrite(FILE *file, struct drgn_error *err)
{
if (err->code == DRGN_ERROR_OS) {

View File

@ -40,14 +40,15 @@ extern struct drgn_error drgn_stop;
/** Not an ELF file. */
extern struct drgn_error drgn_not_elf;
struct string_builder;
/**
* Create a @ref drgn_error from a string that does not need to be duplicated.
* Create a @ref drgn_error with a message from a @ref string_builder.
*
* If there is a failure to allocate memory for the error, @p message is freed
* and @ref drgn_enomem is returned instead.
* This finalizes the string builder.
*/
struct drgn_error *drgn_error_create_nodup(enum drgn_error_code code,
char *message);
struct drgn_error *drgn_error_from_string_builder(enum drgn_error_code code,
struct string_builder *sb);
/** Create a @ref drgn_error from the libelf error indicator. */
struct drgn_error *drgn_error_libelf(void)

View File

@ -1402,14 +1402,8 @@ static struct drgn_error *load_kernel_debug_info(struct drgn_program *prog)
goto err;
if (missing_debug_info.len) {
char *msg;
if (!string_builder_finalize(&missing_debug_info, &msg)) {
err = &drgn_enomem;
goto err;
}
return drgn_error_create_nodup(DRGN_ERROR_MISSING_DEBUG_INFO,
msg);
return drgn_error_from_string_builder(DRGN_ERROR_MISSING_DEBUG_INFO,
&missing_debug_info);
}
return NULL;