Skip to content

improve error handling #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions source/aws_imds_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ struct aws_imds_client *aws_imds_client_new(
}

struct aws_imds_client *client = aws_mem_calloc(allocator, 1, sizeof(struct aws_imds_client));
if (!client) {
return NULL;
}

if (aws_mutex_init(&client->token_lock)) {
goto on_error;
}
Expand Down
3 changes: 0 additions & 3 deletions source/aws_signing.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,6 @@ struct aws_signing_state_aws *aws_signing_state_new(
}

struct aws_signing_state_aws *state = aws_mem_calloc(allocator, 1, sizeof(struct aws_signing_state_aws));
if (!state) {
return NULL;
}

state->allocator = allocator;

Expand Down
6 changes: 0 additions & 6 deletions source/credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ struct aws_credentials *aws_credentials_new(
}

struct aws_credentials *credentials = aws_mem_acquire(allocator, sizeof(struct aws_credentials));
if (credentials == NULL) {
return NULL;
}

AWS_ZERO_STRUCT(*credentials);

Expand Down Expand Up @@ -316,9 +313,6 @@ struct aws_credentials *aws_credentials_new_ecc(
}

struct aws_credentials *credentials = aws_mem_calloc(allocator, 1, sizeof(struct aws_credentials));
if (credentials == NULL) {
return NULL;
}

credentials->allocator = allocator;
credentials->expiration_timepoint_seconds = expiration_timepoint_in_seconds;
Expand Down
9 changes: 0 additions & 9 deletions source/credentials_provider_anonymous.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_anonymous(
struct aws_credentials_provider *provider = aws_mem_calloc(allocator, 1, sizeof(struct aws_credentials_provider));

struct aws_credentials *credentials = aws_credentials_new_anonymous(allocator);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably fine here, but I think it would be better to keep the null check as a design pattern. What if this function starts failing in the future? We should not make assumptions about the return value at this level.

if (credentials == NULL) {
goto on_new_credentials_failure;
}

aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_anonymous_vtable, credentials);

Expand All @@ -53,10 +50,4 @@ struct aws_credentials_provider *aws_credentials_provider_new_anonymous(
}

return provider;

on_new_credentials_failure:

aws_mem_release(allocator, provider);

return NULL;
}
4 changes: 0 additions & 4 deletions source/credentials_provider_cached.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_cached(
&impl,
sizeof(struct aws_credentials_provider_cached));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down
5 changes: 1 addition & 4 deletions source/credentials_provider_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ struct aws_credentials_provider *aws_credentials_provider_new_chain(
const struct aws_credentials_provider_chain_options *options) {

if (options->provider_count == 0) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}

Expand All @@ -160,10 +161,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_chain(
&impl,
sizeof(struct aws_credentials_provider_chain_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down
4 changes: 0 additions & 4 deletions source/credentials_provider_cognito.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_cognito(
&impl,
sizeof(struct aws_credentials_provider_cognito_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down
8 changes: 1 addition & 7 deletions source/credentials_provider_default_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ static struct default_chain_callback_data *s_create_callback_data(
void *user_data) {
struct default_chain_callback_data *callback_data =
aws_mem_calloc(provider->allocator, 1, sizeof(struct default_chain_callback_data));
if (callback_data == NULL) {
return NULL;
}

callback_data->allocator = provider->allocator;
callback_data->default_chain_provider = provider;
callback_data->original_callback = callback;
Expand Down Expand Up @@ -257,10 +255,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_chain_default(
&impl,
sizeof(struct aws_credentials_provider_default_chain_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down
4 changes: 0 additions & 4 deletions source/credentials_provider_delegate.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_delegate(
&impl,
sizeof(struct aws_credentials_provider_delegate_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down
5 changes: 0 additions & 5 deletions source/credentials_provider_ecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_ecs(
sizeof(struct aws_credentials_provider),
&impl,
sizeof(struct aws_credentials_provider_ecs_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down
3 changes: 0 additions & 3 deletions source/credentials_provider_environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_environment(
struct aws_allocator *allocator,
const struct aws_credentials_provider_environment_options *options) {
struct aws_credentials_provider *provider = aws_mem_acquire(allocator, sizeof(struct aws_credentials_provider));
if (provider == NULL) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);

Expand Down
8 changes: 1 addition & 7 deletions source/credentials_provider_imds.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_imds(
&impl,
sizeof(struct aws_credentials_provider_imds_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down Expand Up @@ -133,9 +129,7 @@ static struct imds_provider_user_data *s_imds_provider_user_data_new(

struct imds_provider_user_data *wrapped_user_data =
aws_mem_calloc(imds_provider->allocator, 1, sizeof(struct imds_provider_user_data));
if (wrapped_user_data == NULL) {
goto on_error;
}

if (aws_byte_buf_init(&wrapped_user_data->role, imds_provider->allocator, 100)) {
goto on_error;
}
Expand Down
4 changes: 0 additions & 4 deletions source/credentials_provider_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_process(
&impl,
sizeof(struct aws_credentials_provider_process_impl));

if (!provider) {
goto on_error;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down
4 changes: 0 additions & 4 deletions source/credentials_provider_profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,6 @@ static struct aws_credentials_provider *s_create_profile_based_provider(
&impl,
sizeof(struct aws_credentials_provider_profile_file_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down
3 changes: 0 additions & 3 deletions source/credentials_provider_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_static(
const struct aws_credentials_provider_static_options *options) {

struct aws_credentials_provider *provider = aws_mem_acquire(allocator, sizeof(struct aws_credentials_provider));
if (provider == NULL) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);

Expand Down
3 changes: 0 additions & 3 deletions source/credentials_provider_sts.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_sts(
sizeof(struct aws_credentials_provider_sts_impl));

AWS_LOGF_DEBUG(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "static: creating STS credentials provider");
if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);
Expand Down
4 changes: 1 addition & 3 deletions source/credentials_provider_sts_web_identity.c
Original file line number Diff line number Diff line change
Expand Up @@ -988,9 +988,7 @@ static struct sts_web_identity_parameters *s_parameters_new(

struct sts_web_identity_parameters *parameters =
aws_mem_calloc(allocator, 1, sizeof(struct sts_web_identity_parameters));
if (parameters == NULL) {
return NULL;
}

parameters->allocator = allocator;

bool success = false;
Expand Down
4 changes: 0 additions & 4 deletions source/credentials_provider_x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_x509(
&impl,
sizeof(struct aws_credentials_provider_x509_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down
2 changes: 2 additions & 0 deletions source/credentials_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ struct aws_credentials *aws_parse_credentials_from_json_document(
struct aws_json_value *document_root = aws_json_value_new_from_string(allocator, document);
if (document_root == NULL) {
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to parse document as Json document.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
Copy link
Contributor

@graebm graebm Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should fix this in aws-c-common
aws_json_value_new_from_string() ought to raise an error
and that error should be AWS_ERROR_INVALID_JSON, which would be more useful than INVALID_ARGUMENT

return NULL;
}

Expand All @@ -284,6 +285,7 @@ struct aws_credentials *aws_parse_credentials_from_json_document(
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str(options->top_level_object_name));
if (!top_level_object) {
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "failed to parse top level object in json document.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
goto done;
}
}
Expand Down
4 changes: 0 additions & 4 deletions source/signable.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ struct aws_signable *aws_signable_new_canonical_request(
&impl,
sizeof(struct aws_signable_canonical_request_impl));

if (signable == NULL || impl == NULL) {
return NULL;
}

AWS_ZERO_STRUCT(*signable);
AWS_ZERO_STRUCT(*impl);

Expand Down
4 changes: 0 additions & 4 deletions source/signable_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ struct aws_signable *aws_signable_new_chunk(
aws_mem_acquire_many(
allocator, 2, &signable, sizeof(struct aws_signable), &impl, sizeof(struct aws_signable_chunk_impl));

if (signable == NULL || impl == NULL) {
return NULL;
}

AWS_ZERO_STRUCT(*signable);
AWS_ZERO_STRUCT(*impl);

Expand Down
3 changes: 0 additions & 3 deletions source/signing_result.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ static struct aws_array_list *s_get_or_create_property_list(
}

struct aws_array_list *properties = aws_mem_acquire(result->allocator, sizeof(struct aws_array_list));
if (properties == NULL) {
return NULL;
}

AWS_ZERO_STRUCT(*properties);
struct aws_string *name_copy = aws_string_new_from_string(result->allocator, list_name);
Expand Down
11 changes: 0 additions & 11 deletions tests/credentials_provider_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_mock(
&impl,
sizeof(struct aws_credentials_provider_mock_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down Expand Up @@ -330,10 +326,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_mock_async(
&impl,
sizeof(struct aws_credentials_provider_mock_async_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

Expand Down Expand Up @@ -458,9 +450,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_null(
struct aws_credentials_provider_shutdown_options *shutdown_options) {
struct aws_credentials_provider *provider =
(struct aws_credentials_provider *)aws_mem_acquire(allocator, sizeof(struct aws_credentials_provider));
if (provider == NULL) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);

Expand Down