PO loader: Fix unclosed files and error messages

Fixes #40324.

(cherry picked from commit 47cc202972)

Also removes empty `p_path` as done in 4857648a16.
This commit is contained in:
Rémi Verschelde 2020-07-14 13:16:49 +02:00
parent 4677502d7c
commit a06ee5e763
3 changed files with 16 additions and 15 deletions

View file

@ -33,10 +33,8 @@
#include "core/os/file_access.h" #include "core/os/file_access.h"
#include "core/translation.h" #include "core/translation.h"
RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const String &p_path) { RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
enum Status { enum Status {
STATUS_NONE, STATUS_NONE,
STATUS_READING_ID, STATUS_READING_ID,
STATUS_READING_STRING, STATUS_READING_STRING,
@ -56,6 +54,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
bool skip_this = false; bool skip_this = false;
bool skip_next = false; bool skip_next = false;
bool is_eof = false; bool is_eof = false;
const String path = f->get_path();
while (!is_eof) { while (!is_eof) {
@ -67,7 +66,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
if (status == STATUS_READING_ID) { if (status == STATUS_READING_ID) {
memdelete(f); memdelete(f);
ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: "); ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading 'msgid' at: " + path + ":" + itos(line));
} else { } else {
break; break;
} }
@ -76,9 +75,8 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
if (l.begins_with("msgid")) { if (l.begins_with("msgid")) {
if (status == STATUS_READING_ID) { if (status == STATUS_READING_ID) {
memdelete(f); memdelete(f);
ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: "); ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
} }
if (msg_id != "") { if (msg_id != "") {
@ -98,9 +96,8 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
if (l.begins_with("msgstr")) { if (l.begins_with("msgstr")) {
if (status != STATUS_READING_ID) { if (status != STATUS_READING_ID) {
memdelete(f); memdelete(f);
ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: "); ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' while parsing: " + path + ":" + itos(line));
} }
l = l.substr(6, l.length()).strip_edges(); l = l.substr(6, l.length()).strip_edges();
@ -115,7 +112,10 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
continue; //nothing to read or comment continue; //nothing to read or comment
} }
ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, RES(), p_path + ":" + itos(line) + " Invalid line '" + l + "' while parsing: "); if (!l.begins_with("\"") || status == STATUS_NONE) {
memdelete(f);
ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
}
l = l.substr(1, l.length()); l = l.substr(1, l.length());
//find final quote //find final quote
@ -128,7 +128,10 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
} }
} }
ERR_FAIL_COND_V_MSG(end_pos == -1, RES(), p_path + ":" + itos(line) + " Expected '\"' at end of message while parsing file: "); if (end_pos == -1) {
memdelete(f);
ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
}
l = l.substr(0, end_pos); l = l.substr(0, end_pos);
l = l.c_unescape(); l = l.c_unescape();
@ -141,7 +144,6 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
line++; line++;
} }
f->close();
memdelete(f); memdelete(f);
if (status == STATUS_READING_STRING) { if (status == STATUS_READING_STRING) {
@ -153,7 +155,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
config = msg_str; config = msg_str;
} }
ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + p_path + "."); ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + ".");
Vector<String> configs = config.split("\n"); Vector<String> configs = config.split("\n");
for (int i = 0; i < configs.size(); i++) { for (int i = 0; i < configs.size(); i++) {
@ -190,7 +192,6 @@ RES TranslationLoaderPO::load(const String &p_path, const String &p_original_pat
void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const { void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("po"); p_extensions->push_back("po");
//p_extensions->push_back("mo"); //mo in the future...
} }
bool TranslationLoaderPO::handles_type(const String &p_type) const { bool TranslationLoaderPO::handles_type(const String &p_type) const {

View file

@ -37,7 +37,7 @@
class TranslationLoaderPO : public ResourceFormatLoader { class TranslationLoaderPO : public ResourceFormatLoader {
public: public:
static RES load_translation(FileAccess *f, Error *r_error, const String &p_path = String()); static RES load_translation(FileAccess *f, Error *r_error);
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL); virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
virtual void get_recognized_extensions(List<String> *p_extensions) const; virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const; virtual bool handles_type(const String &p_type) const;

View file

@ -997,7 +997,7 @@ void EditorSettings::setup_language() {
FileAccessMemory *fa = memnew(FileAccessMemory); FileAccessMemory *fa = memnew(FileAccessMemory);
fa->open_custom(data.ptr(), data.size()); fa->open_custom(data.ptr(), data.size());
Ref<Translation> tr = TranslationLoaderPO::load_translation(fa, NULL, "translation_" + String(etl->lang)); Ref<Translation> tr = TranslationLoaderPO::load_translation(fa, NULL);
if (tr.is_valid()) { if (tr.is_valid()) {
tr->set_locale(etl->lang); tr->set_locale(etl->lang);