Skip to contentSkip to conversation

board: dhelectronics: Check pointer before access in dh_get_value_from_eeprom_buffer()

PATCH In review Marek Vasut 3 participants 8 messages Sep 7, 2025 – Sep 17, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 board: dhelectronics: Check pointer before access in dh_get_value_from_eeprom_buffer()

The eip pointer in dh_get_value_from_eeprom_buffer() might be NULL. The current NULL pointer check happens too late, after the eip was accessed in variable assignment. Reorder the two, so the NULL pointer check happens first, and any access second, otherwise the access may trigger a hang or other undefined behavior.

---
Cc: Christoph Niedermaier <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>

---
 board/dhelectronics/common/dh_common.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
board/dhelectronics/common/dh_common.c +6 -3
index 9f8e5754d8c..aeabd617374 100644
@@ -131,14 +131,17 @@ int dh_read_eeprom_id_page(u8 *eeprom_buffer, const char *alias)
 int dh_get_value_from_eeprom_buffer(enum eip_request_values request, u8 *data, int data_len,
 				    struct eeprom_id_page *eip)
 {
-	const char fin_chr = (eip->pl.item_prefix & DH_ITEM_PREFIX_FIN_BIT) ?
-			     DH_ITEM_PREFIX_FIN_FLASHED_CHR : DH_ITEM_PREFIX_FIN_HALF_CHR;
-	const u8 soc_coded = eip->pl.item_prefix & 0xf;
+	char fin_chr;
+	u8 soc_coded;
 	char soc_chr;
 
 	if (!eip)
 		return -EINVAL;
 
+	fin_chr = (eip->pl.item_prefix & DH_ITEM_PREFIX_FIN_BIT) ?
+		  DH_ITEM_PREFIX_FIN_FLASHED_CHR : DH_ITEM_PREFIX_FIN_HALF_CHR;
+	soc_coded = eip->pl.item_prefix & 0xf;
+
 	/* Copy requested data */
 	switch (request) {
 	case DH_MAC0:
Signature · 1 line
2.50.1
#1 lore raw

PATCH 2/2 board: dhelectronics: Use isascii() before isprint() in dh_read_eeprom_id_page()

The isprint() checks printability across all 256 characters, some of the upper 128 characters are printable and produce artifacts on UART. Call isascii() first to only consider the bottom 7bit ASCII characters as printable, and then check their printability using isprint(). This fixes a rare misprint in case the ID page content is uninitialized or corrupted.

---
Cc: Christoph Niedermaier <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>

---
 board/dhelectronics/common/dh_common.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
board/dhelectronics/common/dh_common.c +3 -3
index aeabd617374..e7ee23aa8ce 100644
@@ -88,9 +88,9 @@ int dh_read_eeprom_id_page(u8 *eeprom_buffer, const char *alias)
 	/* Validate header ID */
 	if (eip->hdr.id[0] != 'D' || eip->hdr.id[1] != 'H' || eip->hdr.id[2] != 'E') {
 		printf("%s: Error validating header ID! (got %c%c%c (0x%02x 0x%02x 0x%02x) != expected DHE)\n",
-		       __func__, isprint(eip->hdr.id[0]) ? eip->hdr.id[0] : '.',
-		       isprint(eip->hdr.id[1]) ? eip->hdr.id[1] : '.',
-		       isprint(eip->hdr.id[2]) ? eip->hdr.id[2] : '.',
+		       __func__, (isascii(eip->hdr.id[0]) && isprint(eip->hdr.id[0])) ? eip->hdr.id[0] : '.',
+		       (isascii(eip->hdr.id[1]) && isprint(eip->hdr.id[1])) ? eip->hdr.id[1] : '.',
+		       (isascii(eip->hdr.id[2]) && isprint(eip->hdr.id[2])) ? eip->hdr.id[2] : '.',
 		       eip->hdr.id[0], eip->hdr.id[1], eip->hdr.id[2]);
 		return -EINVAL;
 	}
Signature · 1 line
2.50.1
Reviewed-by on v1 2/2
#2 lore raw

RE: [PATCH 2/2] board: dhelectronics: Use isascii() before isprint() in dh_read_eeprom_id_page()

From: Marek Vasut <[email protected]>
Sent: Sunday, September 7, 2025 3:01 AM

Quoted 34 lines

The isprint() checks printability across all 256 characters, some of the upper 128 characters are printable and produce artifacts on UART. Call isascii() first to only consider the bottom 7bit ASCII characters as printable, and then check their printability using isprint(). This fixes a rare misprint in case the ID page content is uninitialized or corrupted.

---
Cc: Christoph Niedermaier <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>

---
 board/dhelectronics/common/dh_common.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
board/dhelectronics/common/dh_common.c +3 -3
index aeabd617374..e7ee23aa8ce 100644
@@ -88,9 +88,9 @@ int dh_read_eeprom_id_page(u8 *eeprom_buffer, const char *alias)
        /* Validate header ID */
        if (eip->hdr.id[0] != 'D' || eip->hdr.id[1] != 'H' || eip->hdr.id[2] != 'E') {
                printf("%s: Error validating header ID! (got %c%c%c (0x%02x 0x%02x 0x%02x)
 != expected DHE)\n",
-                      __func__, isprint(eip->hdr.id[0]) ? eip->hdr.id[0] : '.',
-                      isprint(eip->hdr.id[1]) ? eip->hdr.id[1] : '.',
-                      isprint(eip->hdr.id[2]) ? eip->hdr.id[2] : '.',
+                      __func__, (isascii(eip->hdr.id[0]) && isprint(eip->hdr.id[0])) ?
 eip->hdr.id[0] : '.',
+                      (isascii(eip->hdr.id[1]) && isprint(eip->hdr.id[1])) ? eip-
 >hdr.id[1] : '.',
+                      (isascii(eip->hdr.id[2]) && isprint(eip->hdr.id[2])) ? eip-
Quoted 1 line

hdr.id[2] : '.',

Quoted 3 lines
                       eip->hdr.id[0], eip->hdr.id[1], eip->hdr.id[2]);
                return -EINVAL;
        }
Reviewed-by on v1 1/2
#3 lore raw
In reply to Marek Vasut

From: Marek Vasut <[email protected]>
Sent: Sunday, September 7, 2025 3:01 AM

Quoted 41 lines

The eip pointer in dh_get_value_from_eeprom_buffer() might be NULL. The current NULL pointer check happens too late, after the eip was accessed in variable assignment. Reorder the two, so the NULL pointer check happens first, and any access second, otherwise the access may trigger a hang or other undefined behavior.

---
Cc: Christoph Niedermaier <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>

---
 board/dhelectronics/common/dh_common.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
board/dhelectronics/common/dh_common.c +6 -3
index 9f8e5754d8c..aeabd617374 100644
@@ -131,14 +131,17 @@ int dh_read_eeprom_id_page(u8 *eeprom_buffer, const char *alias)
 int dh_get_value_from_eeprom_buffer(enum eip_request_values request, u8 *data, int
 data_len,
                                    struct eeprom_id_page *eip)
 {
-       const char fin_chr = (eip->pl.item_prefix & DH_ITEM_PREFIX_FIN_BIT) ?
-                            DH_ITEM_PREFIX_FIN_FLASHED_CHR : DH_ITEM_PREFIX_FIN_HALF_CHR;
-       const u8 soc_coded = eip->pl.item_prefix & 0xf;
+       char fin_chr;
+       u8 soc_coded;
        char soc_chr;
 
        if (!eip)
                return -EINVAL;
 
+       fin_chr = (eip->pl.item_prefix & DH_ITEM_PREFIX_FIN_BIT) ?
+                 DH_ITEM_PREFIX_FIN_FLASHED_CHR : DH_ITEM_PREFIX_FIN_HALF_CHR;
+       soc_coded = eip->pl.item_prefix & 0xf;
+
        /* Copy requested data */
        switch (request) {
Quoted 1 line

case DH_MAC0:

#4 lore raw
In reply to Marek Vasut
Quoted 8 lines · Marek Vasut

The eip pointer in dh_get_value_from_eeprom_buffer() might be NULL. The current NULL pointer check happens too late, after the eip was accessed in variable assignment. Reorder the two, so the NULL pointer check happens first, and any access second, otherwise the access may trigger a hang or other undefined behavior.

[...]

Applied to u-boot/next, thanks!

[1/2] board: dhelectronics: Check pointer before access in dh_get_value_from_eeprom_buffer()

      commit: 1c735620e19e2ae07705cc38da1552ee6a696ff0
[2/2] board: dhelectronics: Use isascii() before isprint() in dh_read_eeprom_id_page()
      commit: fd396316432ad4a0f2998ea9eee9720be0e5d5f8
Signature · 1 line
Tom
#5 lore raw
Quoted 9 lines · Tom Rini
Quoted 7 lines · Marek Vasut

The eip pointer in dh_get_value_from_eeprom_buffer() might be NULL. The current NULL pointer check happens too late, after the eip was accessed in variable assignment. Reorder the two, so the NULL pointer check happens first, and any access second, otherwise the access may trigger a hang or other undefined behavior.

Quoted 8 lines
Quoted 1 line

[...]

Applied to u-boot/next, thanks!

[1/2] board: dhelectronics: Check pointer before access in dh_get_value_from_eeprom_buffer()

       commit: 1c735620e19e2ae07705cc38da1552ee6a696ff0
[2/2] board: dhelectronics: Use isascii() before isprint() in dh_read_eeprom_id_page()
       commit: fd396316432ad4a0f2998ea9eee9720be0e5d5f8

These are bugfixes for master.

#6 lore raw
Quoted 12 lines · Marek Vasut
Quoted 11 lines · Tom Rini
Quoted 8 lines · Marek Vasut

The eip pointer in dh_get_value_from_eeprom_buffer() might be NULL. The current NULL pointer check happens too late, after the eip was accessed in variable assignment. Reorder the two, so the NULL pointer check happens first, and any access second, otherwise the access may trigger a hang or other undefined behavior.

[...]

Quoted 8 lines
Quoted 6 lines

Applied to u-boot/next, thanks!

[1/2] board: dhelectronics: Check pointer before access in dh_get_value_from_eeprom_buffer()

       commit: 1c735620e19e2ae07705cc38da1552ee6a696ff0
[2/2] board: dhelectronics: Use isascii() before isprint() in dh_read_eeprom_id_page()
       commit: fd396316432ad4a0f2998ea9eee9720be0e5d5f8

These are bugfixes for master.

OK, I'll apply it there too now, but please note that cover letters / fixes tags help make it clearer when a fix is important enough to come in to master later in the cycle vs wait for the next release.

Signature · 1 line
Tom
#7 lore raw
In reply to Marek Vasut
Quoted 8 lines · Marek Vasut

The eip pointer in dh_get_value_from_eeprom_buffer() might be NULL. The current NULL pointer check happens too late, after the eip was accessed in variable assignment. Reorder the two, so the NULL pointer check happens first, and any access second, otherwise the access may trigger a hang or other undefined behavior.

[...]

Applied to u-boot/master, thanks!

[1/2] board: dhelectronics: Check pointer before access in dh_get_value_from_eeprom_buffer()

      commit: 5634cf1afcf17fda0136693f02a843aa6b938b2b
[2/2] board: dhelectronics: Use isascii() before isprint() in dh_read_eeprom_id_page()
      commit: 7cd9d2db2627ce65d3c03e4422607b7a86093e8a
Signature · 1 line
Tom