antirez / sds

Simple Dynamic Strings library for C
BSD 2-Clause "Simplified" License
4.88k stars 472 forks source link

Unexpected behaviour #145

Open ghost opened 1 year ago

ghost commented 1 year ago

Hi Salvatore,

I came across some unexpected behaviour while using the sdsMakeRoomFor() and sdsrange() functions. Please have a look below. PS: It might be my one misuse of the library, but curious to hear what you think.

$ echo "test" >> test_file.txt


#include <stdio.h>
#include "sds/sds.h"

#define BUFSIZE 4096

static void expected_behaviour(FILE* const f) {
  sds s = sdsnew("abc");
  s     = sdsMakeRoomFor(s, BUFSIZE);

  while (fgets(s, BUFSIZE, f)) {
    sdsrange(s, 0, 2);
    printf("%s", s); // prints: tes
  }
  printf("\n");
  sdsfree(s);
}

static void unexpected_behaviour(FILE* const f) {
  // Can sdsempty() be used here?
  sds s = sdsMakeRoomFor(sdsempty(), BUFSIZE);

  while (fgets(s, BUFSIZE, f)) {
    // Why isn't sdsrange() working as expected?
    sdsrange(s, 0, 2);
    printf("%s", s); // prints: test\n
  }
  printf("\n");
  sdsfree(s);
}
int main(void) {
  FILE* const f = fopen("test_file.txt", "r");  // assume valid file
  expected_behaviour(f);
  fseek(f, 0, SEEK_SET);
  unexpected_behaviour(f);
  fclose(f)
  return 0;
}
jcorporation commented 1 year ago

After changing a sds string with none-sds-aware functions you should use sdsupatelen(s) to set the correct sdslen (calculated from strlen). Even better is not to use such functions on sds strings.

Your expected behavior function works only, because your init string has the same length as the final output string - the sdslen value is always 3, while the length reported by strlen is changing from 3 to 5 to 3.

Your unexpected behavior function starts and ends with a sdslen of 0 and thats why sdsrange does not strip anything.

You can observe this by adding strlen and sdslen output before and after string modifications.