error: fix handling of error codes
GCC correctly warning as follows:
```
inlined from ‘slp::buffer slp::handler::processError(const slp::Message&, uint8_t)’ at slp_message_handler.cpp:363:16:
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/bits/stl_algobase.h:431:30: warning: ‘void* __builtin_memmove(void*, const void*, long unsigned int)’ reading 2 bytes from a region of size 1 [-Wstringop-overread]
431 | __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
slp_message_handler.cpp: In function ‘slp::buffer slp::handler::processError(const slp::Message&, uint8_t)’:
slp_message_handler.cpp:358:49: note: source object ‘err’ of size 1
```
We were taking the address of a `uint8_t` and copying two bytes of it.
The original code was not correct anyhow because it used a static
OFFSET_ERROR constant. The location of errors is not constant because
it depends on the length of the language tag. Modify the code to
correctly place the 1 byte error type into the 2nd byte of the error
field, which is calculated as immediately after the language tag.
Change-Id: I1bee6bdb1b6301403cf1dff319641587c683f799
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/slp_message_handler.cpp b/slp_message_handler.cpp
index ea90a11..b159ef7 100644
--- a/slp_message_handler.cpp
+++ b/slp_message_handler.cpp
@@ -360,8 +360,12 @@
buffer buff;
buff = slp::handler::internal::prepareHeader(req);
- std::copy_n(&err, slp::response::SIZE_ERROR,
- buff.data() + slp::response::OFFSET_ERROR);
+ static_assert(sizeof(err) == 1, "Errors should be 1 byte.");
+
+ // Since this is network order, the err should go in the 2nd byte of the
+ // error field. This is immediately after the langtag.
+ buff[slp::header::MIN_LEN + req.header.langtag.length() + 1] = err;
+
return buff;
}
} // namespace handler
diff --git a/slp_meta.hpp b/slp_meta.hpp
index 956f18d..538bf9c 100644
--- a/slp_meta.hpp
+++ b/slp_meta.hpp
@@ -53,7 +53,6 @@
constexpr size_t SIZE_URLLENGTH = 2;
constexpr size_t SIZE_AUTH = 1;
-constexpr size_t OFFSET_ERROR = 16;
constexpr size_t OFFSET_SERVICE_LEN = 18;
constexpr size_t OFFSET_SERVICE = 20;
constexpr size_t OFFSET_URL_ENTRY = 18;