From 7be0b4ee6a040dc43c4abb1ba3a276f01d5485c4 Mon Sep 17 00:00:00 2001
From: hinto-janaiyo <hinto.janaiyo@protonmail.com>
Date: Tue, 6 Dec 2022 22:01:36 -0500
Subject: [PATCH] app: check for overflowing user resolution values

---
 src/constants.rs |  6 ++++--
 src/gupax.rs     |  4 ++--
 src/helper.rs    | 10 ++++++++--
 src/main.rs      | 14 +++++++++-----
 4 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/src/constants.rs b/src/constants.rs
index 37439b0..0b36072 100644
--- a/src/constants.rs
+++ b/src/constants.rs
@@ -98,6 +98,8 @@ pub const OS_NAME: &str = "Linux";
 // Gupax
 pub const GUPAX_UPDATE: &str = "Check for updates on Gupax, P2Pool, and XMRig via GitHub's API and upgrade automatically";
 pub const GUPAX_AUTO_UPDATE: &str = "Automatically check for updates at startup";
+pub const GUPAX_SHOULD_RESTART: &str = "Gupax was updated. A restart is recommended but not required";
+pub const GUPAX_UP_TO_DATE: &str = "Gupax is up-to-date";
 #[cfg(not(target_os = "macos"))]
 pub const GUPAX_UPDATE_VIA_TOR: &str = "Update through the Tor network. Tor is embedded within Gupax; a Tor system proxy is not required";
 #[cfg(target_os = "macos")] // Arti library has issues on macOS
@@ -106,8 +108,8 @@ pub const GUPAX_ASK_BEFORE_QUIT: &str = "Ask before quitting Gupax";
 pub const GUPAX_SAVE_BEFORE_QUIT: &str = "Automatically save any changed settings before quitting";
 pub const GUPAX_WIDTH: &str = "Set the width of the Gupax window";
 pub const GUPAX_HEIGHT: &str = "Set the height of the Gupax window";
-pub const GUPAX_LOCK_WIDTH: &str = "Automatically match the height against the width in a 16:10 ratio; aka HEIGHT = WIDTH / 1.6";
-pub const GUPAX_LOCK_HEIGHT: &str = "Automatically match the width against the height in a 16:10 ratio; aka WIDTH = HEIGHT * 1.6";
+pub const GUPAX_LOCK_WIDTH: &str = "Automatically match the HEIGHT against the WIDTH in a 4:3 ratio";
+pub const GUPAX_LOCK_HEIGHT: &str = "Automatically match the WIDTH against the HEIGHT in a 4:3 ratio";
 pub const GUPAX_NO_LOCK: &str = "Allow individual selection of width and height";
 pub const GUPAX_SET: &str = "Set the width/height of the Gupax window to the current values";
 pub const GUPAX_TAB_ABOUT: &str = "Set the tab Gupax starts on to: About";
diff --git a/src/gupax.rs b/src/gupax.rs
index e8baba2..75b2b27 100644
--- a/src/gupax.rs
+++ b/src/gupax.rs
@@ -192,13 +192,13 @@ impl Gupax {
 				Ratio::Width => {
 					let width = self.selected_width as f64;
 					let _height = self.selected_height as f64;
-					let height = (width / 1.6).round();
+					let height = (width / 1.333).round();
 					self.selected_height = height as u16;
 				},
 				Ratio::Height => {
 					let _width = self.selected_width as f64;
 					let height = self.selected_height as f64;
-					let width = (height * 1.6).round();
+					let width = (height * 1.333).round();
 					self.selected_width = width as u16;
 				},
 			}
diff --git a/src/helper.rs b/src/helper.rs
index 6cda25f..00280c5 100644
--- a/src/helper.rs
+++ b/src/helper.rs
@@ -49,7 +49,13 @@ use num_format::{Buffer,Locale};
 use log::*;
 
 //---------------------------------------------------------------------------------------------------- Constants
+// The locale numbers are formatting in is English, which looks like: [1,000]
 const LOCALE: num_format::Locale = num_format::Locale::en;
+// The max amount of bytes of process output we are willing to
+// hold in memory before it's too much and we need to reset.
+const MAX_PROCESS_OUTPUT_BYTES: usize = 56_000_000;
+// Just a little leeway so a reset will go off before the [String] allocates more memory.
+const PROCESS_OUTPUT_LEEWAY: usize = MAX_PROCESS_OUTPUT_BYTES - 1000;
 
 //---------------------------------------------------------------------------------------------------- [Helper] Struct
 // A meta struct holding all the data that gets processed in this thread
@@ -124,7 +130,7 @@ impl Process {
 			// P2Pool log level 1 produces a bit less than 100,000 lines a day.
 			// Assuming each line averages 80 UTF-8 scalars (80 bytes), then this
 			// initial buffer should last around a week (56MB) before resetting.
-			output: Arc::new(Mutex::new(String::with_capacity(56_000_000))),
+			output: Arc::new(Mutex::new(String::with_capacity(MAX_PROCESS_OUTPUT_BYTES))),
 			input: vec![String::new()],
 		}
 	}
@@ -210,7 +216,7 @@ impl Helper {
 	// This will also append a message showing it was reset.
 	fn check_reset_output(output: &Arc<Mutex<String>>, name: ProcessName) {
 		let mut output = output.lock().unwrap();
-		if output.len() > 55_999_000 {
+		if output.len() > PROCESS_OUTPUT_LEEWAY {
 			let name = match name {
 				ProcessName::P2pool => "P2Pool",
 				ProcessName::Xmrig  => "XMRig",
diff --git a/src/main.rs b/src/main.rs
index 1cff40b..4ee74c1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -693,9 +693,13 @@ fn main() {
 	init_logger(now);
 	let mut app = App::new(now);
 	init_auto(&mut app);
-	let initial_window_size = match app.state.gupax.simple {
-		true  => Some(Vec2::new(app.state.gupax.selected_width as f32, app.state.gupax.selected_height as f32)),
-		false => Some(Vec2::new(APP_DEFAULT_WIDTH, APP_DEFAULT_HEIGHT)),
+	let selected_width = app.state.gupax.selected_width as f32;
+	let selected_height = app.state.gupax.selected_height as f32;
+	let initial_window_size = if selected_width > APP_MAX_WIDTH || selected_height > APP_MAX_HEIGHT {
+		warn!("App | Set width or height was greater than the maximum! Starting with the default resolution...");
+		Some(Vec2::new(APP_DEFAULT_WIDTH, APP_DEFAULT_HEIGHT))
+	} else {
+		Some(Vec2::new(app.state.gupax.selected_width as f32, app.state.gupax.selected_height as f32))
 	};
 	let options = init_options(initial_window_size);
 	match clean_dir() {
@@ -942,8 +946,8 @@ impl eframe::App for App {
 					// [Gupax Version]
 					// Is yellow if the user updated and should (but isn't required to) restart.
 					match *self.restart.lock().unwrap() {
-						Restart::Yes => ui.add_sized([width, height], Label::new(RichText::new(&self.name_version).color(YELLOW))).on_hover_text("Gupax was updated. A restart is recommended but not required."),
-						_ => ui.add_sized([width, height], Label::new(&self.name_version)).on_hover_text("Gupax is up-to-date"),
+						Restart::Yes => ui.add_sized([width, height], Label::new(RichText::new(&self.name_version).color(YELLOW))).on_hover_text(GUPAX_SHOULD_RESTART),
+						_ => ui.add_sized([width, height], Label::new(&self.name_version)).on_hover_text(GUPAX_UP_TO_DATE),
 					};
 					ui.separator();
 					// [OS]