apache / apache/celix

Check return value of `vasprintf` and `asprintf`

Open
#491 5 comments 0 reactions 0 assignees View on GitHub
good first issue
Dominant language
C
Stars
193
Forks
99
Avg merge
1m
Merged PRs (30d)
1

Description

According to `man asprintf`, leaving these return value unchecked is dangerous:

> If memory allocation wasn't possible, or some other error occurs, these functions will return -1, and the contents of strp are undefined.

```C
#define _GNU_SOURCE /* See feature_test_macros(7)*/
#include

int asprintf(char **strp, const char *fmt, ...);
int vasprintf(char **strp, const char *fmt, va_list ap);

```
It does fail for various reasons, e.g. EILSEQ ( A wide-character code that does not correspond to a valid character has been detected.)
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html

An example is given by https://stackoverflow.com/questions/65334245/what-is-an-encoding-error-for-sprintf-that-should-return-1:

```C
char buf[42];
wchar_t s[] = { 0xFFFF,49,50,51,0 };
int i = snprintf(buf, sizeof buf, "<%ls>", s);
printf("%d\n", i);
```

`asprintf` is used extensively in various modules, we need to check its usage carefully.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.