Skip to content

Commit

Permalink
psl: fix newline boundary search in relation reformatting
Browse files Browse the repository at this point in the history
When adding an @relation attribute to a forward relation field in the
relation reformatting code that deals with adding missing relation
fields and attributes, we searched for the last character before a
newline to check whether it is a carriage return, but we failed to
check whether the byte offset was a char boundary first. This caused
crashes.

closes prisma/prisma#14895
  • Loading branch information
tomhoule committed Nov 30, 2022
1 parent 31ead21 commit cb87bb6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions psl/psl-core/src/reformat.rs
Expand Up @@ -332,6 +332,11 @@ fn references_argument(inline: walkers::InlineRelationWalker<'_>) -> String {
/// not including the newline in field spans.
fn before_newline(span_end: usize, original_schema: &str) -> usize {
assert!(&original_schema[span_end - 1..span_end] == "\n");

if !original_schema.is_char_boundary(span_end - 2) {
return span_end - 1;
}

match &original_schema[span_end - 2..span_end - 1] {
"\r" => span_end - 2,
_ => span_end - 1,
Expand Down
2 changes: 2 additions & 0 deletions psl/psl/tests/reformat_tests.rs
Expand Up @@ -28,6 +28,8 @@ fn run_reformat_test(test_file_path: &str) {
}

if reformat(&reformatted_text) != reformatted_text {
println!("=== reformatted ===\n{reformatted_text}");
println!("=== reformatted again ===\n{}", reformat(&reformatted_text));
panic!("Reformatting this schema is not idempotent.");
}
}
Expand Down
23 changes: 23 additions & 0 deletions psl/psl/tests/reformatter/regression_thai_trailing_comments.prisma
@@ -0,0 +1,23 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
output = "./generated/client"
}

datasource db {
provider = "sqlserver"
url = "***"
}

model somemodel {
frownyFaceId String
frownyFace frownyface // 囧
id Int @id
}

model frownyface {
somes somemodel[]
}
@@ -0,0 +1,23 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
output = "./generated/client"
}

datasource db {
provider = "sqlserver"
url = "***"
}

model somemodel {
frownyFaceId String
frownyFace frownyface // 囧 @relation(fields: [], references: [])
id Int @id
}

model frownyface {
somes somemodel[]
}

0 comments on commit cb87bb6

Please sign in to comment.