Skip to content

Commit 34ac155

Browse files
committed
dfu: add size check when restoring firmware
1 parent 8b3f7b3 commit 34ac155

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Added
9+
- Size check when restoring firmware via USB/DFU.
10+
711
## [1.0.0-alpha.3] - 2021-04-09
812
### Changed
913
- Print BLE download progress after chunk is complete instead of before.

pybricksdev/dfu.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-License-Identifier: MIT
2-
# Copyright (c) 2019-2020 The Pybricks Authors
2+
# Copyright (c) 2019-2021 The Pybricks Authors
33

44
import errno
55
import os
@@ -93,6 +93,13 @@ def restore_dfu(file: BinaryIO) -> None:
9393
Args:
9494
file: the file that contains the firmware data
9595
"""
96+
file.seek(0, os.SEEK_END)
97+
size = file.tell()
98+
file.seek(0, os.SEEK_SET)
99+
100+
if size < 512:
101+
raise ValueError("File is too small to be a valid firmware file")
102+
96103
try:
97104
# TODO: implement this using pydfu
98105
raise NoBackendError

0 commit comments

Comments
 (0)