Skip to content

Commit f8b4d87

Browse files
committed
Fix rtrim_pos to return an exclusive end pointer
1 parent d0da332 commit f8b4d87

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

include/utils/strings.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ctype.h>
77
#include <stdlib.h>
88
#include <stdbool.h>
9+
#include <string.h>
910

1011
/**
1112
* Safe string duplication
@@ -83,7 +84,9 @@ static inline const char *ltrim_pos(const char *input) {
8384
}
8485

8586
/**
86-
* Returns a pointer to the last printable character in the input string.
87+
* Returns a pointer to the byte _after_ the last printable character
88+
* in the input string. Set the returned pointer to '\0' to terminate
89+
* the string after the last printable character.
8790
*/
8891
static inline const char *rtrim_pos(const char *input, size_t input_size) {
8992
if (!input) {
@@ -100,7 +103,8 @@ static inline const char *rtrim_pos(const char *input, size_t input_size) {
100103
while (end > input && !isgraph((unsigned char)*end)) {
101104
end--;
102105
}
103-
return end;
106+
// Point to the character after the last printable character.
107+
return end + 1;
104108
}
105109

106110
#endif //STRINGS_H

src/utils/strings.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <string.h>
2-
31
#include "utils/strings.h"
42
#include "core/logger.h"
53

0 commit comments

Comments
 (0)