| @@ -203,10 +203,7 @@ impl Session { |
| 203 | 203 | color: bool, |
| 204 | 204 | limits: Limits, |
| 205 | 205 | ) -> io::Result<Outcome> { |
| 206 | | − let Some(added) = source.len().checked_add(2) else { |
| 207 | | − return self.rejected_limit(err, "declaration source is too large"); |
| 208 | | − }; |
| 209 | | − let Some(size) = self.committed.len().checked_add(added) else { |
| 206 | + let Some(size) = appended_source_len(&self.committed, &source) else { |
| 210 | 207 | return self.rejected_limit(err, "declaration source is too large"); |
| 211 | 208 | }; |
| 212 | 209 | if size > limits.session_bytes { |
| @@ -217,6 +214,10 @@ impl Session { |
| 217 | 214 | } |
| 218 | 215 | let previous_len = self.committed.len(); |
| 219 | 216 | let candidate = append_source(&self.committed, &source); |
| 217 | + debug_assert_eq!( |
| 218 | + Some(candidate.len()), |
| 219 | + appended_source_len(&self.committed, &source) |
| 220 | + ); |
| 220 | 221 | self.db.set_text(self.file, candidate.clone()); |
| 221 | 222 | let analysis = self.db.analyze(); |
| 222 | 223 | let mut diagnostics = self.candidate_diagnostics(&analysis, previous_len); |
| @@ -743,6 +744,22 @@ fn read_load_snapshot(path: &Path, limits: Limits) -> Result<Vec<u8>, String> { |
| 743 | 744 | Ok(bytes) |
| 744 | 745 | } |
| 745 | 746 | |
| 747 | +/// Exact byte count of [`append_source`], including only separators and a final |
| 748 | +/// newline that are actually required. Equality with the cap is accepted. |
| 749 | +fn appended_source_len(committed: &str, source: &str) -> Option<usize> { |
| 750 | + let separator = usize::from(!committed.is_empty() && !committed.ends_with('\n')); |
| 751 | + let total = committed |
| 752 | + .len() |
| 753 | + .checked_add(separator)? |
| 754 | + .checked_add(source.len())?; |
| 755 | + let already_terminated = if source.is_empty() { |
| 756 | + separator == 1 || committed.ends_with('\n') |
| 757 | + } else { |
| 758 | + source.ends_with('\n') |
| 759 | + }; |
| 760 | + total.checked_add(usize::from(!already_terminated)) |
| 761 | +} |
| 762 | + |
| 746 | 763 | fn append_source(committed: &str, source: &str) -> String { |
| 747 | 764 | let mut candidate = String::with_capacity(committed.len() + source.len() + 2); |
| 748 | 765 | candidate.push_str(committed); |
| @@ -1096,23 +1113,72 @@ struct DiagnosticWriter<'a> { |
| 1096 | 1113 | out: &'a mut dyn Write, |
| 1097 | 1114 | remaining: usize, |
| 1098 | 1115 | truncated: bool, |
| 1116 | + /// At most the prefix of one UTF-8 scalar, across arbitrary Write chunks. |
| 1117 | + utf8_carry: Vec<u8>, |
| 1118 | +} |
| 1119 | + |
| 1120 | +impl DiagnosticWriter<'_> { |
| 1121 | + fn write_text(&mut self, text: &str) -> io::Result<()> { |
| 1122 | + if text.len() <= self.remaining { |
| 1123 | + self.out.write_all(text.as_bytes())?; |
| 1124 | + self.remaining -= text.len(); |
| 1125 | + return Ok(()); |
| 1126 | + } |
| 1127 | + let mut boundary = self.remaining; |
| 1128 | + while !text.is_char_boundary(boundary) { |
| 1129 | + boundary -= 1; |
| 1130 | + } |
| 1131 | + self.out.write_all(&text.as_bytes()[..boundary])?; |
| 1132 | + self.truncated = true; |
| 1133 | + Err(io::Error::other("diagnostic limit")) |
| 1134 | + } |
| 1099 | 1135 | } |
| 1100 | 1136 | |
| 1101 | 1137 | impl Write for DiagnosticWriter<'_> { |
| 1102 | | − fn write(&mut self, buffer: &[u8]) -> io::Result<usize> { |
| 1138 | + fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { |
| 1103 | 1139 | if self.truncated { |
| 1104 | 1140 | return Err(io::Error::other("diagnostic limit")); |
| 1105 | 1141 | } |
| 1106 | | − if buffer.len() > self.remaining { |
| 1107 | | − let prefix = self.remaining; |
| 1108 | | − self.out.write_all(&buffer[..prefix])?; |
| 1109 | | − self.remaining = 0; |
| 1110 | | − self.truncated = true; |
| 1111 | | − return Err(io::Error::other("diagnostic limit")); |
| 1142 | + let original_len = bytes.len(); |
| 1143 | + let mut input = bytes; |
| 1144 | + |
| 1145 | + if !self.utf8_carry.is_empty() { |
| 1146 | + let Some(width) = utf8_sequence_len(self.utf8_carry[0]) else { |
| 1147 | + return Err(invalid_diagnostic_utf8()); |
| 1148 | + }; |
| 1149 | + let needed = width - self.utf8_carry.len(); |
| 1150 | + let take = needed.min(input.len()); |
| 1151 | + self.utf8_carry.extend_from_slice(&input[..take]); |
| 1152 | + input = &input[take..]; |
| 1153 | + if self.utf8_carry.len() < width { |
| 1154 | + return Ok(original_len); |
| 1155 | + } |
| 1156 | + if width > self.remaining { |
| 1157 | + self.utf8_carry.clear(); |
| 1158 | + self.truncated = true; |
| 1159 | + return Err(io::Error::other("diagnostic limit")); |
| 1160 | + } |
| 1161 | + let scalar = std::mem::take(&mut self.utf8_carry); |
| 1162 | + let Ok(scalar) = std::str::from_utf8(&scalar) else { |
| 1163 | + return Err(invalid_diagnostic_utf8()); |
| 1164 | + }; |
| 1165 | + self.write_text(scalar)?; |
| 1112 | 1166 | } |
| 1113 | | − self.out.write_all(buffer)?; |
| 1114 | | − self.remaining -= buffer.len(); |
| 1115 | | − Ok(buffer.len()) |
| 1167 | + |
| 1168 | + match std::str::from_utf8(input) { |
| 1169 | + Ok(text) => self.write_text(text)?, |
| 1170 | + Err(error) if error.error_len().is_none() => { |
| 1171 | + // The complete prefix can be emitted now. Keep only the final |
| 1172 | + // incomplete scalar prefix (1 to 3 bytes) for the next write. |
| 1173 | + let end = error.valid_up_to(); |
| 1174 | + let valid = |
| 1175 | + std::str::from_utf8(&input[..end]).map_err(|_| invalid_diagnostic_utf8())?; |
| 1176 | + self.write_text(valid)?; |
| 1177 | + self.utf8_carry.extend_from_slice(&input[end..]); |
| 1178 | + } |
| 1179 | + Err(_) => return Err(invalid_diagnostic_utf8()), |
| 1180 | + } |
| 1181 | + Ok(original_len) |
| 1116 | 1182 | } |
| 1117 | 1183 | |
| 1118 | 1184 | fn flush(&mut self) -> io::Result<()> { |
| @@ -1120,6 +1186,23 @@ impl Write for DiagnosticWriter<'_> { |
| 1120 | 1186 | } |
| 1121 | 1187 | } |
| 1122 | 1188 | |
| 1189 | +fn invalid_diagnostic_utf8() -> io::Error { |
| 1190 | + io::Error::new( |
| 1191 | + io::ErrorKind::InvalidData, |
| 1192 | + "diagnostic output is invalid UTF-8", |
| 1193 | + ) |
| 1194 | +} |
| 1195 | + |
| 1196 | +fn utf8_sequence_len(first: u8) -> Option<usize> { |
| 1197 | + match first { |
| 1198 | + 0x00..=0x7f => Some(1), |
| 1199 | + 0xc2..=0xdf => Some(2), |
| 1200 | + 0xe0..=0xef => Some(3), |
| 1201 | + 0xf0..=0xf4 => Some(4), |
| 1202 | + _ => None, |
| 1203 | + } |
| 1204 | +} |
| 1205 | + |
| 1123 | 1206 | fn render_diagnostics( |
| 1124 | 1207 | sources: &SourceDb, |
| 1125 | 1208 | diagnostics: &[Diagnostic], |
| @@ -1135,6 +1218,7 @@ fn render_diagnostics( |
| 1135 | 1218 | out, |
| 1136 | 1219 | remaining: limit, |
| 1137 | 1220 | truncated: false, |
| 1221 | + utf8_carry: Vec::new(), |
| 1138 | 1222 | }; |
| 1139 | 1223 | let result = louise_diag::render(sources, diagnostics, color, &mut limited); |
| 1140 | 1224 | (result, limited.truncated) |
| @@ -1202,7 +1286,9 @@ mod tests { |
| 1202 | 1286 | _ => panic!("expression was not prepared: {error}"), |
| 1203 | 1287 | }; |
| 1204 | 1288 | let entry_name = &prepared.program.fns[prepared.entry as usize].name; |
| 1205 | | − assert_eq!(entry_name, "+repl.__louise_repl_0"); |
| 1289 | + // The earlier `:type twice` uses/disposes of _0; this expression gets |
| 1290 | + // the next private name. Neither becomes a later committed definition. |
| 1291 | + assert_eq!(entry_name, "+repl.__louise_repl_1"); |
| 1206 | 1292 | drop(prepared); |
| 1207 | 1293 | let (outcome, error) = submit(&mut session, "__louise_repl_0", Mode::Type); |
| 1208 | 1294 | assert!(matches!(outcome, Outcome::Rejected)); |
| @@ -1604,18 +1690,20 @@ mod tests { |
| 1604 | 1690 | #[test] |
| 1605 | 1691 | fn program_output_limit_is_bounded_and_the_next_expression_still_works() { |
| 1606 | 1692 | let (out, err) = run_with_limits( |
| 1607 | | − b"import std/io\nio.println \"1234\"\nio.eprintln \"abcde\"\n1\n", |
| 1693 | + b"import std/io\n{\n io.println \"1234\"\n io.eprintln \"abcde\"\n}\n1\n", |
| 1608 | 1694 | Limits { |
| 1609 | 1695 | program_output_bytes: 5, |
| 1610 | 1696 | ..Limits::default() |
| 1611 | 1697 | }, |
| 1612 | 1698 | ); |
| 1613 | | − // stdout's five bytes fit exactly. B1 then preflights the six stderr |
| 1614 | | − // bytes against the same entry budget; C's writer adds no second charge. |
| 1615 | | − // No partial `()` or eprintln value is emitted, and the next entry resets. |
| 1699 | + // Both writes belong to this one block/entry. stdout's five bytes fit; |
| 1700 | + // B1 preflights the six stderr bytes against the same budget, and C's |
| 1701 | + // writer adds no second charge. No returned `()` follows that failed |
| 1702 | + // entry, while a separate valid unit entry must still print its value. |
| 1616 | 1703 | assert_eq!(out, "1234\n1\n"); |
| 1617 | 1704 | assert!(err.contains("OutputBytes"), "{err}"); |
| 1618 | 1705 | assert!(!err.contains("abcde"), "{err}"); |
| 1706 | + assert!(!err.contains("stack backtrace"), "{err}"); |
| 1619 | 1707 | } |
| 1620 | 1708 | |
| 1621 | 1709 | #[test] |
| @@ -1640,12 +1728,28 @@ mod tests { |
| 1640 | 1728 | ); |
| 1641 | 1729 | |
| 1642 | 1730 | let mut session = Session::new(Path::new(".")); |
| 1643 | | − let oversized = format!("fn too_big :: -> Int {{ {} }}\n", "1 + ".repeat(40) + "1"); |
| 1731 | + let limits = small_limits(); |
| 1732 | + let prefix = "//"; |
| 1733 | + let definition = "\nfn capped :: -> () { () }\n"; |
| 1734 | + let padding = limits.session_bytes - prefix.len() - definition.len(); |
| 1735 | + let exact = format!("{}{}{}", prefix, "x".repeat(padding), definition); |
| 1736 | + assert_eq!(exact.len(), limits.session_bytes); |
| 1644 | 1737 | let mut errors = Vec::new(); |
| 1645 | 1738 | let outcome = session |
| 1646 | | − .commit_source(oversized, &mut errors, false, small_limits()) |
| 1739 | + .commit_source(exact.clone(), &mut errors, false, limits) |
| 1740 | + .unwrap(); |
| 1741 | + assert!( |
| 1742 | + matches!(outcome, Outcome::Committed), |
| 1743 | + "{}", |
| 1744 | + String::from_utf8_lossy(&errors) |
| 1745 | + ); |
| 1746 | + let before = session.committed.clone(); |
| 1747 | + let outcome = session |
| 1748 | + .commit_source(exact, &mut errors, false, limits) |
| 1647 | 1749 | .unwrap(); |
| 1648 | 1750 | assert!(matches!(outcome, Outcome::Rejected)); |
| 1751 | + assert_eq!(session.committed, before, "failed append changed source"); |
| 1752 | + assert!(String::from_utf8(errors).unwrap().contains("session limit")); |
| 1649 | 1753 | session.reset(); |
| 1650 | 1754 | assert_eq!(session.next_wrapper, 0); |
| 1651 | 1755 | assert!(session.committed.is_empty()); |
| @@ -1670,7 +1774,8 @@ mod tests { |
| 1670 | 1774 | InputLine::TooLong |
| 1671 | 1775 | )); |
| 1672 | 1776 | assert!( |
| 1673 | | − matches!(read_bounded_line(&mut too_long, 3).unwrap(), InputLine::Text(line) if line == "next\n") |
| 1777 | + // LF is excluded from the limit: `next` is 4 bytes, not 3. |
| 1778 | + matches!(read_bounded_line(&mut too_long, 4).unwrap(), InputLine::Text(line) if line == "next\n") |
| 1674 | 1779 | ); |
| 1675 | 1780 | let mut invalid = io::Cursor::new(vec![0xff, b'\n']); |
| 1676 | 1781 | assert!(matches!( |
| @@ -1680,6 +1785,39 @@ mod tests { |
| 1680 | 1785 | } |
| 1681 | 1786 | |
| 1682 | 1787 | #[test] |
| 1788 | + fn diagnostic_writer_carries_split_utf8_and_never_emits_a_partial_scalar() { |
| 1789 | + let expected = "é─ok"; |
| 1790 | + let mut out = Vec::new(); |
| 1791 | + { |
| 1792 | + let mut writer = DiagnosticWriter { |
| 1793 | + out: &mut out, |
| 1794 | + remaining: expected.len(), |
| 1795 | + truncated: false, |
| 1796 | + utf8_carry: Vec::new(), |
| 1797 | + }; |
| 1798 | + for chunk in expected.as_bytes().chunks(1) { |
| 1799 | + writer.write_all(chunk).unwrap(); |
| 1800 | + } |
| 1801 | + } |
| 1802 | + assert_eq!(out, expected.as_bytes()); |
| 1803 | + |
| 1804 | + let mut out = Vec::new(); |
| 1805 | + let truncated = { |
| 1806 | + let mut writer = DiagnosticWriter { |
| 1807 | + out: &mut out, |
| 1808 | + remaining: 1, |
| 1809 | + truncated: false, |
| 1810 | + utf8_carry: Vec::new(), |
| 1811 | + }; |
| 1812 | + writer.write("é".as_bytes()).unwrap_err(); |
| 1813 | + writer.truncated |
| 1814 | + }; |
| 1815 | + assert!(truncated); |
| 1816 | + assert_eq!(out, b"", "a partial scalar must stay in the cap's carry"); |
| 1817 | + assert_eq!(String::from_utf8(out).unwrap(), ""); |
| 1818 | + } |
| 1819 | + |
| 1820 | + #[test] |
| 1683 | 1821 | fn diagnostics_have_an_explicit_truncation_diagnostic() { |
| 1684 | 1822 | let mut sources = SourceDb::default(); |
| 1685 | 1823 | let file = sources.add(SourceFile::new(DISPLAY_PATH, "bad")); |