A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/stlink-org/stlink/pull/1214 below:

STM32L-Discovery (STM32L152R8T6) can't write firmware to chip with st-flash on Windows 10 · Issue #1214 · stlink-org/stlink · GitHub

Commandline output:

mmap() size_t overflow for file mb.bin

Expected/description:

Debugging revealed that problem is in file common.c, function map_file, in code below:

  if (sizeof(st.st_size) != sizeof(size_t)) {
    // on 32 bit systems, check if there is an overflow
    if (st.st_size > (off_t)SSIZE_MAX) {
      fprintf(stderr, "mmap() size_t overflow for file %s\n", path);
      goto on_error;
    }
  }

Type of st.st_size is off_t, which means long int. Value is size of firmware file and for me is 602 bytes.
SSIZE_MAX defines somewhere in libc headers. It means maximum size of signed integer. But in my system it compiled in long long int. Not 4 bytes long int as off_t, but 8 bytes long long int! So there is overflow in casting types without warnings during compile. As a result, instead of:
if (602 > 2^32-1) {
I got:
if (602 > -1) {
And then function returned with error message.

Proposed solution, which works on my computer:
In header section of common.c define macro OFF_T_MAX such a way:

#ifndef OFF_T_MAX
#define OFF_T_MAX 1073741824 // long int max value
#endif

In map_file function replace SSIZE_MAX by newly defined OFF_T_MAX.

Also please note that SSIZE_MAX defined in unistd.h in such a way:

#define ssize_t int
#ifndef SSIZE_MAX
#define SSIZE_MAX ((sizeof(ssize_t) == 4) ? 1073741824 : ‭4611686018427387904‬)
#endif

There is an error in numbers. SSIZE_MAX is max. value of signed int and must be equal 0x400... . But here it equals 0x7FFF ... in both cases, which is true for UNsigned int. For signed int both values are -1. But in my system SSIZE_MAX defined in libc, so this macro is not used.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4