Pārlūkot izejas kodu

Fix potential race condition when determining the package name

gingerBill 2 gadi atpakaļ
vecāks
revīzija
edb23db2ae
2 mainītis faili ar 6 papildinājumiem un 1 dzēšanām
  1. 5 1
      src/parser.cpp
  2. 1 0
      src/parser.hpp

+ 5 - 1
src/parser.cpp

@@ -4949,7 +4949,7 @@ gb_internal void parser_add_foreign_file_to_process(Parser *p, AstPackage *pkg,
 
 
 // NOTE(bill): Returns true if it's added
-gb_internal AstPackage *try_add_import_path(Parser *p, String const &path, String const &rel_path, TokenPos pos, PackageKind kind = Package_Normal) {
+gb_internal AstPackage *try_add_import_path(Parser *p, String path, String const &rel_path, TokenPos pos, PackageKind kind = Package_Normal) {
 	String const FILE_EXT = str_lit(".odin");
 
 	MUTEX_GUARD_BLOCK(&p->imported_files_mutex) {
@@ -4958,6 +4958,8 @@ gb_internal AstPackage *try_add_import_path(Parser *p, String const &path, Strin
 		}
 	}
 
+	path = copy_string(permanent_allocator(), path);
+
 	AstPackage *pkg = gb_alloc_item(permanent_allocator(), AstPackage);
 	pkg->kind = kind;
 	pkg->fullpath = path;
@@ -5715,6 +5717,7 @@ gb_internal ParseFileError process_imported_file(Parser *p, ImportedFile importe
 			array_add(&pkg->files, file);
 		}
 
+		mutex_lock(&pkg->name_mutex);
 		if (pkg->name.len == 0) {
 			pkg->name = file->package_name;
 		} else if (pkg->name != file->package_name) {
@@ -5726,6 +5729,7 @@ gb_internal ParseFileError process_imported_file(Parser *p, ImportedFile importe
 				syntax_error(tok, "Different package name, expected '%.*s', got '%.*s'", LIT(pkg->name), LIT(file->package_name));
 			}
 		}
+		mutex_unlock(&pkg->name_mutex);
 
 		p->total_line_count.fetch_add(file->tokenizer.line_count);
 		p->total_token_count.fetch_add(file->tokens.count);

+ 1 - 0
src/parser.hpp

@@ -175,6 +175,7 @@ struct AstPackage {
 	BlockingMutex         files_mutex;
 	BlockingMutex         foreign_files_mutex;
 	BlockingMutex         type_and_value_mutex;
+	BlockingMutex         name_mutex;
 
 	// NOTE(bill): This must be a MPMCQueue
 	MPMCQueue<AstPackageExportedEntity> exported_entity_queue;