Check ftell return values

This commit is contained in:
Campbell Barton 2015-06-05 11:46:01 +10:00
parent b2e5c017a1
commit aca40295e8
Notes: blender-bot 2023-02-14 09:02:42 +01:00
Referenced by issue #46647, Extrude Individual Faces + tweak in Last Operator panel = Crash
Referenced by issue #44971, Image Editor Bug
Referenced by issue #44965, particles over a subdivided cube with displace won't render
Referenced by issue #44966, no speed nor memory footprint improvement after selective node compilation commit
4 changed files with 39 additions and 17 deletions

View File

@ -46,7 +46,7 @@ void txt_set_undostate (int u);
int txt_get_undostate (void);
struct Text *BKE_text_add (struct Main *bmain, const char *name);
int txt_extended_ascii_as_utf8(char **str);
int BKE_text_reload (struct Text *text);
bool BKE_text_reload(struct Text *text);
struct Text *BKE_text_load_ex(struct Main *bmain, const char *file, const char *relpath,
const bool is_internal);
struct Text *BKE_text_load (struct Main *bmain, const char *file, const char *relpath);

View File

@ -330,7 +330,7 @@ static void text_from_buf(Text *text, const unsigned char *buffer, const int len
text->curc = text->selc = 0;
}
int BKE_text_reload(Text *text)
bool BKE_text_reload(Text *text)
{
FILE *fp;
int len;
@ -339,13 +339,24 @@ int BKE_text_reload(Text *text)
char str[FILE_MAX];
BLI_stat_t st;
if (!text->name) return 0;
if (!text->name) {
return false;
}
BLI_strncpy(str, text->name, FILE_MAX);
BLI_path_abs(str, G.main->name);
fp = BLI_fopen(str, "r");
if (fp == NULL) return 0;
if (fp == NULL) {
return false;
}
fseek(fp, 0L, SEEK_END);
len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if (UNLIKELY(len == -1)) {
fclose(fp);
return false;
}
/* free memory: */
@ -363,11 +374,6 @@ int BKE_text_reload(Text *text)
MEM_freeN(text->undo_buf);
init_undo_text(text);
fseek(fp, 0L, SEEK_END);
len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
buffer = MEM_mallocN(len, "text_buffer");
/* under windows fread can return less than len bytes because
* of CR stripping */
@ -385,7 +391,7 @@ int BKE_text_reload(Text *text)
text_from_buf(text, buffer, len);
MEM_freeN(buffer);
return 1;
return true;
}
Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const bool is_internal)
@ -402,8 +408,18 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const
BLI_path_abs(str, relpath);
fp = BLI_fopen(str, "r");
if (fp == NULL) return NULL;
if (fp == NULL) {
return NULL;
}
fseek(fp, 0L, SEEK_END);
len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if (UNLIKELY(len == -1)) {
fclose(fp);
return NULL;
}
ta = BKE_libblock_alloc(bmain, ID_TXT, BLI_path_basename(str));
ta->id.us = 1;
@ -423,10 +439,6 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const
/* clear undo buffer */
init_undo_text(ta);
fseek(fp, 0L, SEEK_END);
len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
buffer = MEM_mallocN(len, "text_buffer");
/* under windows fread can return less than len bytes because

View File

@ -296,6 +296,11 @@ LinkNode *BLI_file_read_as_lines(const char *name)
size = (size_t)ftell(fp);
fseek(fp, 0, SEEK_SET);
if (UNLIKELY(size == (size_t)-1)) {
fclose(fp);
return NULL;
}
buf = MEM_mallocN(size, "file_as_lines");
if (buf) {
size_t i, last = 0;

View File

@ -539,6 +539,11 @@ static void *read_file_data(char *filename, int *r_len)
*r_len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if (*r_len == -1) {
fclose(fp);
return NULL;
}
data = MEM_mallocN(*r_len, "read_file_data");
if (!data) {
*r_len = -1;