(task_database_cue) Don't use malloc

This commit is contained in:
twinaphex 2020-08-26 19:46:13 +02:00
parent 00b4d62460
commit 143224e09a
1 changed files with 28 additions and 40 deletions

View File

@ -493,7 +493,7 @@ int cue_find_track(const char *cue_path, bool first,
{ {
int rv; int rv;
intfstream_info_t info; intfstream_info_t info;
char *tmp_token = (char*)malloc(MAX_TOKEN_LEN); char tmp_token[MAX_TOKEN_LEN];
char last_file[PATH_MAX_LENGTH]; char last_file[PATH_MAX_LENGTH];
char cue_dir[PATH_MAX_LENGTH]; char cue_dir[PATH_MAX_LENGTH];
intfstream_t *fd = NULL; intfstream_t *fd = NULL;
@ -528,7 +528,7 @@ int cue_find_track(const char *cue_path, bool first,
rv = -EINVAL; rv = -EINVAL;
while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) while (get_token(fd, tmp_token, sizeof(tmp_token)) > 0)
{ {
if (string_is_equal_noncase(tmp_token, "FILE")) if (string_is_equal_noncase(tmp_token, "FILE"))
{ {
@ -546,27 +546,27 @@ int cue_find_track(const char *cue_path, bool first,
goto clean; goto clean;
} }
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token));
fill_pathname_join(last_file, cue_dir, fill_pathname_join(last_file, cue_dir,
tmp_token, sizeof(last_file)); tmp_token, sizeof(last_file));
file_size = intfstream_get_file_size(last_file); file_size = intfstream_get_file_size(last_file);
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token));
} }
else if (string_is_equal_noncase(tmp_token, "TRACK")) else if (string_is_equal_noncase(tmp_token, "TRACK"))
{ {
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token));
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token));
is_data = !string_is_equal_noncase(tmp_token, "AUDIO"); is_data = !string_is_equal_noncase(tmp_token, "AUDIO");
++track; ++track;
} }
else if (string_is_equal_noncase(tmp_token, "INDEX")) else if (string_is_equal_noncase(tmp_token, "INDEX"))
{ {
int m, s, f; int m, s, f;
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token));
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token));
if (sscanf(tmp_token, "%02d:%02d:%02d", &m, &s, &f) < 3) if (sscanf(tmp_token, "%02d:%02d:%02d", &m, &s, &f) < 3)
{ {
@ -607,13 +607,11 @@ int cue_find_track(const char *cue_path, bool first,
rv = 0; rv = 0;
clean: clean:
free(tmp_token);
intfstream_close(fd); intfstream_close(fd);
free(fd); free(fd);
return rv; return rv;
error: error:
free(tmp_token);
if (fd) if (fd)
{ {
intfstream_close(fd); intfstream_close(fd);
@ -625,27 +623,26 @@ error:
bool cue_next_file(intfstream_t *fd, bool cue_next_file(intfstream_t *fd,
const char *cue_path, char *path, uint64_t max_len) const char *cue_path, char *path, uint64_t max_len)
{ {
char tmp_token[MAX_TOKEN_LEN];
char cue_dir[PATH_MAX_LENGTH]; char cue_dir[PATH_MAX_LENGTH];
bool rv = false; bool rv = false;
char *tmp_token = (char*)malloc(MAX_TOKEN_LEN);
cue_dir[0] = '\0'; cue_dir[0] = '\0';
fill_pathname_basedir(cue_dir, cue_path, sizeof(cue_dir)); fill_pathname_basedir(cue_dir, cue_path, sizeof(cue_dir));
tmp_token[0] = '\0'; tmp_token[0] = '\0';
while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) while (get_token(fd, tmp_token, sizeof(tmp_token)) > 0)
{ {
if (string_is_equal_noncase(tmp_token, "FILE")) if (string_is_equal_noncase(tmp_token, "FILE"))
{ {
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token));
fill_pathname_join(path, cue_dir, tmp_token, (size_t)max_len); fill_pathname_join(path, cue_dir, tmp_token, (size_t)max_len);
rv = true; rv = true;
break; break;
} }
} }
free(tmp_token);
return rv; return rv;
} }
@ -654,7 +651,7 @@ int gdi_find_track(const char *gdi_path, bool first,
{ {
int rv; int rv;
intfstream_info_t info; intfstream_info_t info;
char *tmp_token = (char*)malloc(MAX_TOKEN_LEN); char tmp_token[MAX_TOKEN_LEN];
intfstream_t *fd = NULL; intfstream_t *fd = NULL;
uint64_t largest = 0; uint64_t largest = 0;
int size = -1; int size = -1;
@ -683,20 +680,20 @@ int gdi_find_track(const char *gdi_path, bool first,
rv = -EINVAL; rv = -EINVAL;
/* Skip track count */ /* Skip track count */
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token));
/* Track number */ /* Track number */
while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) while (get_token(fd, tmp_token, sizeof(tmp_token)) > 0)
{ {
/* Offset */ /* Offset */
if (get_token(fd, tmp_token, MAX_TOKEN_LEN) <= 0) if (get_token(fd, tmp_token, sizeof(tmp_token)) <= 0)
{ {
errno = EINVAL; errno = EINVAL;
goto error; goto error;
} }
/* Mode */ /* Mode */
if (get_token(fd, tmp_token, MAX_TOKEN_LEN) <= 0) if (get_token(fd, tmp_token, sizeof(tmp_token)) <= 0)
{ {
errno = EINVAL; errno = EINVAL;
goto error; goto error;
@ -705,7 +702,7 @@ int gdi_find_track(const char *gdi_path, bool first,
mode = atoi(tmp_token); mode = atoi(tmp_token);
/* Sector size */ /* Sector size */
if (get_token(fd, tmp_token, MAX_TOKEN_LEN) <= 0) if (get_token(fd, tmp_token, sizeof(tmp_token)) <= 0)
{ {
errno = EINVAL; errno = EINVAL;
goto error; goto error;
@ -714,7 +711,7 @@ int gdi_find_track(const char *gdi_path, bool first,
size = atoi(tmp_token); size = atoi(tmp_token);
/* File name */ /* File name */
if (get_token(fd, tmp_token, MAX_TOKEN_LEN) <= 0) if (get_token(fd, tmp_token, sizeof(tmp_token)) <= 0)
{ {
errno = EINVAL; errno = EINVAL;
goto error; goto error;
@ -750,7 +747,7 @@ int gdi_find_track(const char *gdi_path, bool first,
} }
/* Disc offset (not used?) */ /* Disc offset (not used?) */
if (get_token(fd, tmp_token, MAX_TOKEN_LEN) <= 0) if (get_token(fd, tmp_token, sizeof(tmp_token)) <= 0)
{ {
errno = EINVAL; errno = EINVAL;
goto error; goto error;
@ -758,13 +755,11 @@ int gdi_find_track(const char *gdi_path, bool first,
} }
clean: clean:
free(tmp_token);
intfstream_close(fd); intfstream_close(fd);
free(fd); free(fd);
return rv; return rv;
error: error:
free(tmp_token);
if (fd) if (fd)
{ {
intfstream_close(fd); intfstream_close(fd);
@ -776,28 +771,22 @@ error:
bool gdi_next_file(intfstream_t *fd, const char *gdi_path, bool gdi_next_file(intfstream_t *fd, const char *gdi_path,
char *path, uint64_t max_len) char *path, uint64_t max_len)
{ {
char tmp_token[MAX_TOKEN_LEN];
bool rv = false; bool rv = false;
char *tmp_token = (char*)malloc(MAX_TOKEN_LEN);
int64_t offset = -1;
tmp_token[0] = '\0'; tmp_token[0] = '\0';
/* Skip initial track count */ /* Skip initial track count */
offset = intfstream_tell(fd); if (intfstream_tell(fd) == 0)
if (offset == 0) get_token(fd, tmp_token, sizeof(tmp_token));
get_token(fd, tmp_token, MAX_TOKEN_LEN);
/* Track number */ get_token(fd, tmp_token, sizeof(tmp_token)); /* Track number */
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token)); /* Offset */
/* Offset */ get_token(fd, tmp_token, sizeof(tmp_token)); /* Mode */
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token)); /* Sector size */
/* Mode */
get_token(fd, tmp_token, MAX_TOKEN_LEN);
/* Sector size */
get_token(fd, tmp_token, MAX_TOKEN_LEN);
/* File name */ /* File name */
if (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) if (get_token(fd, tmp_token, sizeof(tmp_token)) > 0)
{ {
char gdi_dir[PATH_MAX_LENGTH]; char gdi_dir[PATH_MAX_LENGTH];
@ -810,9 +799,8 @@ bool gdi_next_file(intfstream_t *fd, const char *gdi_path,
rv = true; rv = true;
/* Disc offset */ /* Disc offset */
get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, sizeof(tmp_token));
} }
free(tmp_token);
return rv; return rv;
} }