Skip to content

Commit 185a25d

Browse files
committed
haiku: improve C++ glue
- Defend against NULL input buffer. - Use auto and static_cast to clean up type conversions. - Use BString::CopyInto API instead of raw memcpy. - NUL terminate the buffer if possible.
1 parent e6bc833 commit 185a25d

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

haiku/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "iana-time-zone-haiku"
33
description = "iana-time-zone support crate for Haiku OS"
4-
version = "0.1.2"
4+
version = "0.1.3"
55
authors = ["René Kijewski <crates.io@k6i.de>"]
66
repository = "https://github.com/strawlab/iana-time-zone"
77
license = "MIT OR Apache-2.0"

haiku/src/implementation.cc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ size_t iana_time_zone_haiku_get_tz(char *buf, size_t buf_size) {
1515
try {
1616
static_assert(sizeof(char) == sizeof(uint8_t), "Illegal char size");
1717

18-
if (buf_size == 0) {
18+
if (!buf || buf_size == 0) {
1919
return 0;
2020
}
2121

@@ -32,24 +32,23 @@ size_t iana_time_zone_haiku_get_tz(char *buf, size_t buf_size) {
3232
}
3333

3434
BString bname(tz.ID());
35-
int32_t ilength(bname.Length());
36-
if (ilength <= 0) {
35+
auto raw_length = bname.Length();
36+
if (raw_length <= 0) {
3737
return 0;
3838
}
3939

40-
size_t length(ilength);
40+
size_t length = static_cast<size_t>(raw_length);
4141
if (length > buf_size) {
4242
return 0;
4343
}
4444

45-
// BString::String() returns a borrowed string.
46-
// https://www.haiku-os.org/docs/api/classBString.html#ae4fe78b06c8e3310093b80305e14ba87
47-
const char *sname(bname.String());
48-
if (!sname) {
49-
return 0;
45+
bname.CopyInto(buf, 0, raw_length);
46+
47+
// Optionally, NUL-terminate the buffer if there's room:
48+
if (length < buf_size) {
49+
buf[length] = '\0';
5050
}
5151

52-
std::memcpy(buf, sname, length);
5352
return length;
5453
} catch (...) {
5554
return 0;

0 commit comments

Comments
 (0)