Skip to content

Commit 5b74e25

Browse files
ellenspthinkyhead
andauthored
🔨 BSD string workaround (MarlinFirmware#26532)
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
1 parent f02fa63 commit 5b74e25

File tree

7 files changed

+154
-15
lines changed

7 files changed

+154
-15
lines changed

Marlin/src/HAL/LINUX/HAL.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
*
2121
*/
22+
2223
#ifdef __PLAT_LINUX__
2324

2425
#include "../../inc/MarlinConfig.h"
@@ -57,4 +58,49 @@ uint16_t MarlinHAL::adc_value() {
5758

5859
void MarlinHAL::reboot() { /* Reset the application state and GPIO */ }
5960

61+
// ------------------------
62+
// BSD String
63+
// ------------------------
64+
65+
/**
66+
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
67+
*
68+
* Permission to use, copy, modify, and distribute this software for any
69+
* purpose with or without fee is hereby granted, provided that the above
70+
* copyright notice and this permission notice appear in all copies.
71+
*
72+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
73+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
74+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
75+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
76+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
77+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
78+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
79+
*/
80+
81+
#ifndef HAS_LIBBSD
82+
83+
/**
84+
* Copy string src to buffer dst of size dsize. At most dsize-1
85+
* chars will be copied. Always NUL terminates (unless dsize == 0).
86+
* Returns strlen(src); if retval >= dsize, truncation occurred.
87+
*/
88+
size_t MarlinHAL::_strlcpy(char *dst, const char *src, size_t dsize) {
89+
const char *osrc = src;
90+
size_t nleft = dsize;
91+
92+
// Copy as many bytes as will fit.
93+
if (nleft != 0) while (--nleft != 0) if ((*dst++ = *src++) == '\0') break;
94+
95+
// Not enough room in dst, add NUL and traverse rest of src.
96+
if (nleft == 0) {
97+
if (dsize != 0) *dst = '\0'; // NUL-terminate dst
98+
while (*src++) { /* nada */ }
99+
}
100+
101+
return (src - osrc - 1); // count does not include NUL
102+
}
103+
104+
#endif // HAS_LIBBSD
105+
60106
#endif // __PLAT_LINUX__

Marlin/src/HAL/LINUX/HAL.h

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#include <iostream>
2727
#include <stdint.h>
2828
#include <stdarg.h>
29+
30+
#ifdef HAS_LIBBSD
31+
#include <bsd/string.h>
32+
#endif
33+
2934
#undef min
3035
#undef max
3136
#include <algorithm>
@@ -162,4 +167,13 @@ class MarlinHAL {
162167
}
163168

164169
static void set_pwm_frequency(const pin_t, int) {}
170+
171+
#ifndef HAS_LIBBSD
172+
/**
173+
* Redirect missing strlcpy here
174+
*/
175+
static size_t _strlcpy(char *dst, const char *src, size_t dsize);
176+
#define strlcpy hal._strlcpy
177+
#endif
178+
165179
};

Marlin/src/HAL/LINUX/include/Arduino.h

-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828

2929
#include <pinmapping.h>
3030

31-
#define strlcpy(A, B, C) strncpy(A, B, (C) - 1)
32-
#define strlcpy_P(A, B, C) strncpy_P(A, B, (C) - 1)
33-
3431
#define HIGH 0x01
3532
#define LOW 0x00
3633

Marlin/src/HAL/NATIVE_SIM/HAL.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
/**
24+
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
25+
*
26+
* Permission to use, copy, modify, and distribute this software for any
27+
* purpose with or without fee is hereby granted, provided that the above
28+
* copyright notice and this permission notice appear in all copies.
29+
*
30+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
31+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
32+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
33+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
34+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
35+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
36+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37+
*/
38+
39+
#ifdef __PLAT_NATIVE_SIM__
40+
41+
#ifndef HAS_LIBBSD
42+
43+
#include "HAL.h"
44+
45+
/**
46+
* Copy string src to buffer dst of size dsize. At most dsize-1
47+
* chars will be copied. Always NUL terminates (unless dsize == 0).
48+
* Returns strlen(src); if retval >= dsize, truncation occurred.
49+
*/
50+
size_t MarlinHAL::_strlcpy(char *dst, const char *src, size_t dsize) {
51+
const char *osrc = src;
52+
size_t nleft = dsize;
53+
54+
// Copy as many bytes as will fit.
55+
if (nleft != 0) while (--nleft != 0) if ((*dst++ = *src++) == '\0') break;
56+
57+
// Not enough room in dst, add NUL and traverse rest of src.
58+
if (nleft == 0) {
59+
if (dsize != 0) *dst = '\0'; // NUL-terminate dst
60+
while (*src++) { /* nada */ }
61+
}
62+
63+
return (src - osrc - 1); // count does not include NUL
64+
}
65+
66+
#endif // HAS_LIBBSD
67+
#endif // __PLAT_NATIVE_SIM__

Marlin/src/HAL/NATIVE_SIM/HAL.h

+10
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,14 @@ class MarlinHAL {
263263
analogWrite(pin, v);
264264
}
265265

266+
static void set_pwm_frequency(const pin_t, int) {}
267+
268+
#ifndef HAS_LIBBSD
269+
/**
270+
* Redirect missing strlcpy here
271+
*/
272+
static size_t _strlcpy(char *dst, const char *src, size_t dsize);
273+
#define strlcpy hal._strlcpy
274+
#endif
275+
266276
};

Marlin/src/inc/Conditionals_post.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
// Extras for CI testing
3131
#endif
3232

33+
// Arduino IDE with Teensy Additions
34+
#ifdef TEENSYDUINO
35+
#undef max
36+
#define max(a,b) ((a)>(b)?(a):(b))
37+
#undef min
38+
#define min(a,b) ((a)<(b)?(a):(b))
39+
#undef NOT_A_PIN // Override Teensyduino legacy CapSense define work-around
40+
#define NOT_A_PIN 0 // For PINS_DEBUGGING
41+
#endif
42+
3343
// ADC
3444
#ifdef BOARD_ADC_VREF_MV
3545
#define ADC_VREF_MV BOARD_ADC_VREF_MV
@@ -64,16 +74,6 @@
6474
#undef OTA_FIRMWARE_UPDATE
6575
#endif
6676

67-
#ifdef TEENSYDUINO
68-
#undef max
69-
#define max(a,b) ((a)>(b)?(a):(b))
70-
#undef min
71-
#define min(a,b) ((a)<(b)?(a):(b))
72-
73-
#undef NOT_A_PIN // Override Teensyduino legacy CapSense define work-around
74-
#define NOT_A_PIN 0 // For PINS_DEBUGGING
75-
#endif
76-
7777
/**
7878
* Axis lengths and center
7979
*/

ini/native.ini

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ build_src_filter = ${common.default_src_filter} +<src/HAL/LINUX>
3636
[simulator_common]
3737
platform = native
3838
framework =
39-
build_flags = ${common.build_flags} -std=gnu++17 -D__PLAT_NATIVE_SIM__ -DU8G_HAL_LINKS -I/usr/include/SDL2 -IMarlin -IMarlin/src/HAL/NATIVE_SIM/u8g
39+
build_flags = ${common.build_flags} -std=gnu++17 -D__PLAT_NATIVE_SIM__ -DU8G_HAL_LINKS
40+
-I/usr/include/SDL2 -IMarlin -IMarlin/src/HAL/NATIVE_SIM/u8g
4041
build_src_flags = -Wall -Wno-expansion-to-defined -Wno-deprecated-declarations -Wcast-align
4142
release_flags = -g0 -O3 -flto
4243
debug_build_flags = -fstack-protector-strong -g -g3 -ggdb
@@ -99,6 +100,7 @@ build_flags = ${simulator_linux.build_flags} ${simulator_linux.release_flags}
99100
[simulator_macos]
100101
build_unflags = -lGL -fstack-protector-strong
101102
build_flags =
103+
-DHAS_LIBBSD
102104
-I/opt/local/include
103105
-I/opt/local/include/freetype2
104106
-I/opt/local/include/SDL2/
@@ -135,5 +137,8 @@ custom_gcc = g++
135137
[env:simulator_windows]
136138
extends = simulator_common
137139
build_src_flags = ${simulator_common.build_src_flags} -fpermissive
138-
build_flags = ${simulator_common.build_flags} ${simulator_common.debug_build_flags} -IC:\\msys64\\mingw64\\include\\SDL2 -fno-stack-protector -Wl,-subsystem,windows -ldl -lmingw32 -lSDL2main -lSDL2 -lSDL2_net -lopengl32 -lssp
140+
build_flags = ${simulator_common.build_flags} ${simulator_common.debug_build_flags}
141+
-IC:\\msys64\\mingw64\\include\\SDL2 -fno-stack-protector -Wl,-subsystem,windows
142+
-ldl -lmingw32 -lSDL2main -lSDL2 -lSDL2_net -lopengl32 -lssp
143+
-DHAS_LIBBSD
139144
build_type = debug

0 commit comments

Comments
 (0)