commit 76236b8d9bdb5eb16fb32e725fd436349d21fcfa
parent cc5b9ee6387d48300815f184b5f21b8be583189c
Author: nibo <nibo@relim.de>
Date: Tue, 15 Jul 2025 15:50:17 +0200
Free memory in case of error in src/core.c
Diffstat:
3 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/src/chordpro.c b/src/chordpro.c
@@ -4858,6 +4858,7 @@ cho_context_init(
ctx->grid.bar_line_symbol_in_line_count = 0;
ctx->grid.tokens_per_cell = 0;
ctx->grid.expected_tokens_per_cell = 0;
+ ctx->config = config;
ctx->songs = emalloc(sizeof(struct ChoSong *));
ctx->songs[ctx->so] = cho_song_new(ctx);
if (!ctx->songs[ctx->so]) {
@@ -4869,7 +4870,6 @@ cho_context_init(
ctx->transpose_history[ctx->th] = 0;
ctx->transpose = &ctx->transpose_history[ctx->th];
ctx->th++;
- ctx->config = config;
ctx->songs[ctx->so]->sections = emalloc((ctx->se+1) * sizeof(struct ChoSection *));
ctx->songs[ctx->so]->sections[ctx->se] = cho_section_new();
ctx->songs[ctx->so]->sections[ctx->se]->lines = emalloc(sizeof(struct ChoLine *));
diff --git a/src/core.c b/src/core.c
@@ -210,30 +210,33 @@ str_trim(const char *str)
int begin = 0;
int end = 0;
int len = (int)strlen(str);
- for (int i=0; i<len; i++) {
+ int i, k;
+
+ for (i = 0; i<len; i++) {
if (
str[i] == ' ' ||
str[i] == '\n' ||
str[i] == '\t' ||
str[i] == '\r'
- )
+ ) {
begin++;
- else
+ } else {
break;
+ }
}
- for (int i=len-1; i>=0; i--) {
+ for (i = len-1; i>=0; i--) {
if (
str[i] == ' '||
str[i] == '\n' ||
str[i] == '\t' ||
str[i] == '\r'
- )
+ ) {
end++;
- else
+ } else {
break;
+ }
}
- int k = 0;
- for (int i=0; i<len; i++) {
+ for (i = 0, k = 0; i<len; i++) {
if (i >= begin && i < len - end) {
trimmed = erealloc(trimmed, (k+1) * sizeof(char));
trimmed[k] = str[i];
@@ -248,14 +251,12 @@ str_trim(const char *str)
char *
str_remove_leading_whitespace(const char *str)
{
- int i = 0;
- while (str[i] == ' ' || str[i] == '\t') {
- i++;
- }
+ int i;
+ for (i = 0; str[i] == ' ' || str[i] == '\t'; i++);
return strdup(&str[i]);
}
-/* INFO: Allows a and b to be NULL. */
+/* INFO: Difference to strcmp is it allows a and b to be NULL */
int
str_compare(const char *a, const char *b)
{
@@ -482,6 +483,7 @@ filepath_dirname(const char *path)
{
char *dirname;
int i, end = 0;
+
for (i = 0; path[i]; i++) {
if (path[i] == '/') {
end = i;
@@ -571,14 +573,14 @@ size_create(const char *str)
d = strtod(str, &endptr);
if (str == endptr || errno == ERANGE) {
LOG_DEBUG("strtod failed.");
- return NULL;
+ goto ERR;
}
size->d = d;
size->type = SIZE_TYPE_POINT;
if (len > 1 && str[len-1] == '%') {
if (size->d < 1.0 || size->d > 100.0) {
util_log(NULL, 0, LOG_ERR, "invalid percentage.");
- return NULL;
+ goto ERR;
}
size->d = d / 100.0;
size->type = SIZE_TYPE_PERCENT;
@@ -590,6 +592,9 @@ size_create(const char *str)
size->type = SIZE_TYPE_EX;
}
return size;
+ ERR:
+ free(size);
+ return NULL;
}
struct Size *
diff --git a/src/core.h b/src/core.h
@@ -18,6 +18,8 @@
#define COLOR_BOLD_BLUE "\033[1;34m"
#define COLOR_RESET "\033[0m"
+#define MAX_STRINGS 12
+
enum Alignment {
ALIGNMENT_LEFT,
ALIGNMENT_CENTER,
@@ -297,8 +299,8 @@ struct Size {
struct StringDiagram {
char *name;
int8_t base_fret;
- int8_t frets[12];
- int8_t fingers[12];
+ int8_t frets[MAX_STRINGS];
+ int8_t fingers[MAX_STRINGS];
};
struct KeyboardDiagram {