|
@@ -127,7 +127,7 @@ Alongside constructs which exist in the C++ STL and standard library, some funct
|
127
|
127
|
|
128
|
128
|
This module offers an interface to easily reason through heap-allocated C-strings, which may be struct members. The functions ensure that an initialized C-string is either `NULL` or a `char*` to a valid null-terminated string. In this paradigm, testing if a string is empty/non-existent is as easy as testing the pointer itself, and no unnecessary memory has to be allocated to simply represent an empty C-string. This works particularly well, as `NULL` is a valid argument for `free()`.
|
129
|
129
|
|
130
|
|
-The only minor cost is a branch in C-string assignment (`cstring_asn`), in order to accept a valid, intialized string (which may be `NULL`).
|
|
130
|
+The only minor cost is a branch in C-string assignment (`cstring_asn`), in order to accept a valid, intialized string (which may be `NULL`). The reasoning behind this is to allow implementing the assignment of a struct to another struct easily; since it is possible for the string to be `NULL`, we don't want `cstring_asn` to break assuming the value is allocated.
|
131
|
131
|
|
132
|
132
|
Example usage:
|
133
|
133
|
```c
|
|
@@ -148,7 +148,24 @@ int main() {
|
148
|
148
|
}
|
149
|
149
|
```
|
150
|
150
|
|
151
|
|
-There
|
|
151
|
+A small set of functions are also avaliable within this header, which are much more useful globally. `safestrcpy` and `safestrcat` allow specifying a limit to protect from overwriting buffers, where `lim - 1` characters are copied max.
|
|
152
|
+
|
|
153
|
+Example usage:
|
|
154
|
+```c
|
|
155
|
+#include <cstl/cstring.h>
|
|
156
|
+#define BUF_MAX 8
|
|
157
|
+char buf[BUF_MAX];
|
|
158
|
+
|
|
159
|
+int main() {
|
|
160
|
+ char s[] = "I can't not easily overrun the buffer above";
|
|
161
|
+
|
|
162
|
+ //Perform a safestrcpy to prevent it!
|
|
163
|
+ safestrcpy(buf, s, BUF_MAX);
|
|
164
|
+
|
|
165
|
+ //Let's check to see if our buffer is safe: (prints "I can't")
|
|
166
|
+ puts(buf);
|
|
167
|
+}
|
|
168
|
+```
|
152
|
169
|
|
153
|
170
|
More can be found in [cstring.h](./include/cstl/cstring.h).
|
154
|
171
|
|