1
0

Let's put some pointer safety. Like checks for NULL and actually freeing them in case of errors.

This commit is contained in:
Alexander Andreev 2024-03-24 05:35:39 +04:00
parent 16e0149802
commit 1afd678fd4
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
2 changed files with 10 additions and 0 deletions

View File

@ -32,9 +32,12 @@ int audio_init(audio_t *aud, int channels, int sample_rate, int frame_size) {
aud->buffer_size = frame_size * channels; aud->buffer_size = frame_size * channels;
aud->buffer = (audio_sample_t *)malloc(aud->buffer_size * sizeof(audio_sample_t)); aud->buffer = (audio_sample_t *)malloc(aud->buffer_size * sizeof(audio_sample_t));
if (aud->buffer == NULL)
return -1;
if ((pa_err = Pa_StartStream(aud->stream)) != paNoError) { if ((pa_err = Pa_StartStream(aud->stream)) != paNoError) {
fprintf(stderr, "Cannot start a PortAudio stream: %s\n", Pa_GetErrorText(pa_err)); fprintf(stderr, "Cannot start a PortAudio stream: %s\n", Pa_GetErrorText(pa_err));
free(aud->buffer);
return -1; return -1;
} }

View File

@ -105,12 +105,16 @@ int crypt_store_key(const crypt_key_t *const k, FILE *const pub, FILE *const sec
unsigned char *crypt_hello(const crypt_key_t *const own) { unsigned char *crypt_hello(const crypt_key_t *const own) {
unsigned char *hello = (unsigned char *)malloc(CRYPT_HELLO_LEN * sizeof(unsigned char)); unsigned char *hello = (unsigned char *)malloc(CRYPT_HELLO_LEN * sizeof(unsigned char));
if (hello == NULL)
return NULL;
if (crypt_key_export_public(own, (char *const)hello) == -1) { if (crypt_key_export_public(own, (char *const)hello) == -1) {
free(hello);
return NULL; return NULL;
} }
if (crypto_sign_detached(hello+CRYPT_PKEY_HEXLEN, NULL, (const unsigned char *)hello, CRYPT_PKEY_HEXLEN, own->sign_sec) == -1) { if (crypto_sign_detached(hello+CRYPT_PKEY_HEXLEN, NULL, (const unsigned char *)hello, CRYPT_PKEY_HEXLEN, own->sign_sec) == -1) {
free(hello);
return NULL; return NULL;
} }
@ -167,8 +171,11 @@ int crypt_session_init(crypt_session_t *const s, const crypt_key_t *const own, c
unsigned char *crypt_session_encrypt(crypt_session_t *const s, const unsigned char *const m, unsigned long long mlen, unsigned long long *clen) { unsigned char *crypt_session_encrypt(crypt_session_t *const s, const unsigned char *const m, unsigned long long mlen, unsigned long long *clen) {
unsigned char *c = (unsigned char *)malloc((mlen + crypto_aead_aegis256_ABYTES) * sizeof(unsigned char)); unsigned char *c = (unsigned char *)malloc((mlen + crypto_aead_aegis256_ABYTES) * sizeof(unsigned char));
if (c == NULL)
return NULL;
if (crypto_aead_aegis256_encrypt(c, clen, m, mlen, NULL, 0, NULL, s->nonce, s->tx) == -1) { if (crypto_aead_aegis256_encrypt(c, clen, m, mlen, NULL, 0, NULL, s->nonce, s->tx) == -1) {
free(c);
return NULL; return NULL;
} }