Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behaviour #145

Open
ghost opened this issue Oct 18, 2022 · 1 comment
Open

Unexpected behaviour #145

ghost opened this issue Oct 18, 2022 · 1 comment

Comments

@ghost
Copy link

ghost commented Oct 18, 2022

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
Copy link

jcorporation commented Nov 15, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant