From 26f431c9443f411c8c00f005600910c167a30517 Mon Sep 17 00:00:00 2001
From: Joerg Wunsch <j@uriah.heep.sax.de>
Date: Sat, 23 Jul 2022 10:26:17 +0200
Subject: [PATCH] Handle invalid -U file format specifiers for input

The file format specifiers 'h', 'd', 'o', and 'b' are only valid for
outputting data. Reject them with a proper error message when
attempting to use them for input.
---
 src/update.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/src/update.c b/src/update.c
index d3c208fc..8642f39d 100644
--- a/src/update.c
+++ b/src/update.c
@@ -278,6 +278,34 @@ int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd, enum updateflags f
      * write the selected device memory using data from a file; first
      * read the data from the specified file
      */
+    const char *name = 0;
+    switch (upd->format) {
+    case FMT_HEX:
+      name = "hex";
+      break;
+
+    case FMT_DEC:
+      name = "decimal";
+      break;
+
+    case FMT_OCT:
+      name = "octal";
+      break;
+
+    case FMT_BIN:
+      name = "binary";
+      break;
+
+    default:
+      // no action needed
+      break;
+    }
+    if (name != 0) {
+      avrdude_message(MSG_INFO,
+                      "%s: Invalid file format '%s' for input\n",
+                      progname, name, upd->filename);
+      return -1;
+    }
     if (quell_progress < 2) {
       avrdude_message(MSG_INFO, "%s: reading input file \"%s\"\n",
                       progname,