Commit 9a3c4f962cac2b1ae7e1321a97adaac384b2072b

Thomas de Grivel 2024-07-29T21:21:10

fix str_init_slice and str_sw_pos_to_uw

diff --git a/libkc3/buf.c b/libkc3/buf.c
index 3cc9c6b..b87a86a 100644
--- a/libkc3/buf.c
+++ b/libkc3/buf.c
@@ -732,19 +732,30 @@ s_str * buf_read_to_str (s_buf *buf, s_str *dest)
   sw size;
   assert(buf);
   assert(dest);
-  if (buf->rpos > buf->wpos)
+  if (buf->rpos > buf->wpos) {
+    err_puts("buf_read_to_str: buf->rpos > buf->wpos");
+    assert(! "buf_read_to_str: buf->rpos > buf->wpos");
     return NULL;
-  if (buf->wpos > buf->size)
+  }
+  if (buf->wpos > buf->size) {
+    err_puts("buf_read_to_str: buf->wpos > buf->size");
+    assert(! "buf_read_to_str: buf->wpos > buf->size");
     return NULL;
+  }
   size = buf->wpos - buf->rpos;
-  if (size == 0) {
+  if (! size) {
     str_init_empty(dest);
     return dest;
   }
-  if (! str_init_alloc(dest, size, buf->ptr.pchar + buf->rpos))
+  if (! str_init_alloc(dest, size, buf->ptr.pchar + buf->rpos)) {
+    err_puts("buf_read_to_str: str_init_alloc");
+    assert(! "buf_read_to_str: str_init_alloc");
     return NULL;
+  }
   r = buf_ignore(buf, size);
   if (r < 0) {
+    err_puts("buf_read_to_str: buf_ignore");
+    assert(! "buf_read_to_str: buf_ignore");
     str_clean(dest);
     return NULL;
   }
diff --git a/libkc3/str.c b/libkc3/str.c
index 6809d5a..79a4885 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -338,8 +338,11 @@ s_str * str_init_slice (s_str *str, const s_str *src, sw start, sw end)
     assert(! "str_init_slice: invalid positions");
     return NULL;
   }
-  if (! buf_read_to_str(&buf, &tmp))
+  if (! buf_read_to_str(&buf, &tmp)) {
+    err_puts("str_init_slice: buf_read_to_str");
+    assert(! "str_init_slice: buf_read_to_str");
     return NULL;
+  }
   *str = tmp;
   return str;
 }
@@ -721,7 +724,7 @@ uw * str_sw_pos_to_uw (sw pos, uw max_pos, uw *dest)
   }
   else {
     if (max_pos > SW_MAX || pos >= (sw) -max_pos)
-      *dest = max_pos - pos + 1;
+      *dest = max_pos + pos + 1;
     else {
       err_write_1("str_sw_pos_to_uw: index too low: ");
       err_inspect_sw(&pos);