recent changes
This commit is contained in:
@@ -4,8 +4,7 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* ----------------------------- strbuf ------------------------------ */
|
||||
|
||||
@@ -25,9 +24,15 @@ void sb_free(strbuf *s) {
|
||||
|
||||
int sb_append(strbuf *s, const char *data, size_t n) {
|
||||
if (n == 0) return 0;
|
||||
if (s->len + n + 1 > s->cap) {
|
||||
size_t ncap = s->cap ? s->cap * 2 : 64;
|
||||
while (ncap < s->len + n + 1) ncap *= 2;
|
||||
if (!data) return -1;
|
||||
if (n > SIZE_MAX - s->len - 1) return -1;
|
||||
size_t need = s->len + n + 1;
|
||||
if (need > s->cap) {
|
||||
size_t ncap = s->cap ? s->cap : 64;
|
||||
while (ncap < need) {
|
||||
if (ncap > SIZE_MAX / 2) { ncap = need; break; }
|
||||
ncap *= 2;
|
||||
}
|
||||
char *nb = (char *)realloc(s->buf, ncap);
|
||||
if (!nb) return -1;
|
||||
s->buf = nb;
|
||||
@@ -40,6 +45,7 @@ int sb_append(strbuf *s, const char *data, size_t n) {
|
||||
}
|
||||
|
||||
int sb_append_str(strbuf *s, const char *str) {
|
||||
if (!str) return 0;
|
||||
return sb_append(s, str, strlen(str));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user