diff --git a/lib/chck b/lib/chck index 56f2a99..8de0be8 160000 --- a/lib/chck +++ b/lib/chck @@ -1 +1 @@ -Subproject commit 56f2a9931ec0daf4ffab89791a2da75791159555 +Subproject commit 8de0be809d20c60ab3e6f41a224db10dd4d399e2 diff --git a/src/pi9.c b/src/pi9.c index 4218699..ac4a29e 100644 --- a/src/pi9.c +++ b/src/pi9.c @@ -110,6 +110,7 @@ static const struct { static inline bool write_qid(struct pi9_qid *qid, struct pi9_stream *stream) { + assert(qid && stream); return (chck_buffer_write_int(&qid->type, sizeof(qid->type), &stream->out) && chck_buffer_write_int(&qid->vers, sizeof(qid->vers), &stream->out) && chck_buffer_write_int(&qid->path, sizeof(qid->path), &stream->out)); @@ -118,6 +119,7 @@ write_qid(struct pi9_qid *qid, struct pi9_stream *stream) static inline bool read_qid(struct pi9_qid *qid, struct pi9_stream *stream) { + assert(qid && stream); return (chck_buffer_read_int(&qid->type, sizeof(qid->type), &stream->in) && chck_buffer_read_int(&qid->vers, sizeof(qid->vers), &stream->in) && chck_buffer_read_int(&qid->path, sizeof(qid->path), &stream->in)); @@ -126,6 +128,8 @@ read_qid(struct pi9_qid *qid, struct pi9_stream *stream) static inline bool read_stat(struct pi9_stat *stat, struct pi9_stream *stream) { + assert(stat && stream); + uint16_t size; if (!chck_buffer_read_int(&size, sizeof(size), &stream->in) || !chck_buffer_read_int(&stat->type, sizeof(stat->type), &stream->in) || @@ -248,7 +252,7 @@ op_Tauth(struct pi9 *pi9, uint16_t tag, struct pi9_stream *stream) if (!chck_buffer_write_int(&size, sizeof(size), &stream->out) || !chck_buffer_write_int((uint8_t[]){Rauth}, sizeof(uint8_t), &stream->out) || !chck_buffer_write_int(&tag, sizeof(tag), &stream->out) || - !write_qid(qid, stream)) + (qid && !write_qid(qid, stream))) goto err_write; return true; diff --git a/src/pi9_string.h b/src/pi9_string.h index e38075e..2429cae 100644 --- a/src/pi9_string.h +++ b/src/pi9_string.h @@ -11,6 +11,8 @@ struct pi9_string { bool is_heap; }; +#define PSTRE(x) (x ? x : "") + static inline bool pi9_cstr_is_empty(const char *data) { @@ -21,14 +23,14 @@ static inline bool pi9_cstr_ends_with(const char *a, const char *b) { const size_t lena = (a ? strlen(a) : 0), lenb = (b ? strlen(b) : 0); - return (lena >= lenb && !memcmp(a + lena - lenb, b, lenb)); + return (lena >= lenb && !memcmp(a + lena - lenb, PSTRE(b), lenb)); } static inline bool pi9_cstr_starts_with(const char *a, const char *b) { const size_t lena = (a ? strlen(a) : 0), lenb = (b ? strlen(b) : 0); - return (lena >= lenb && !memcmp(a, b, lenb)); + return (lena >= lenb && !memcmp(PSTRE(a), PSTRE(b), lenb)); } static inline bool @@ -53,41 +55,43 @@ static inline bool pi9_string_ends_with_cstr(const struct pi9_string *a, const char *cstr) { const size_t len = (cstr ? strlen(cstr) : 0); - return (a->size >= len && !memcmp(a->data + a->size - len, cstr, len)); + return (a->size >= len && !memcmp(a->data + a->size - len, PSTRE(cstr), len)); } static inline bool pi9_string_starts_with_cstr(const struct pi9_string *a, const char *cstr) { const size_t len = (cstr ? strlen(cstr) : 0); - return (a->size >= len && !memcmp(a->data, cstr, len)); + return (a->size >= len && !memcmp(a->data, PSTRE(cstr), len)); } static inline bool pi9_string_ends_with(const struct pi9_string *a, const struct pi9_string *b) { - return (a->size >= b->size && !memcmp(a->data + a->size - b->size, b->data, b->size)); + return (a->size >= b->size && !memcmp(a->data + a->size - b->size, PSTRE(b->data), b->size)); } static inline bool pi9_string_starts_with(const struct pi9_string *a, const struct pi9_string *b) { - return (a->size >= b->size && !memcmp(a->data, b->data, b->size)); + return (a->size >= b->size && !memcmp(PSTRE(a->data), PSTRE(b->data), b->size)); } static inline bool pi9_string_eq(const struct pi9_string *a, const struct pi9_string *b) { - return (a->data == b->data) || (a->size == b->size && !memcmp(a->data, b->data, a->size)); + return (a->data == b->data) || (a->size == b->size && !memcmp(PSTRE(a->data), PSTRE(b->data), a->size)); } static inline bool pi9_string_eq_cstr(const struct pi9_string *a, const char *cstr) { const size_t len = (cstr ? strlen(cstr) : 0); - return (len == a->size) && (cstr == a->data || !memcmp(a->data, cstr, a->size)); + return (len == a->size) && (cstr == a->data || !memcmp(PSTRE(a->data), PSTRE(cstr), a->size)); } +#undef PSTRE + void pi9_string_release(struct pi9_string *string); bool pi9_string_set_cstr(struct pi9_string *string, const char *data, bool is_heap); bool pi9_string_set_cstr_with_length(struct pi9_string *string, const char *data, size_t len, bool is_heap);