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

Empty string을 저장(set)하고 나서 조회(get)하면 NULL 리턴하는 이슈 #360

Open
jhpark816 opened this issue Feb 20, 2025 · 2 comments
Assignees

Comments

@jhpark816
Copy link
Contributor

jhpark816 commented Feb 20, 2025

🔍 Description

  • Empty string을 set 연산으로 저장한 후에
  • 다시 get하여 조회하면 emtpry string이 아니라 NULL을 리턴한다.
  • 이러한 동작이 맞는 지를 검토한다.

⏰ Implementation Idea

@jhpark816 jhpark816 changed the title Empty string을 set하고 나서 조회(get)하면 NULL 리턴하면 이슈 Empty string을 저장(set)하고 나서 조회(get)하면 NULL 리턴하는 이슈 Feb 20, 2025
@jhpark816
Copy link
Contributor Author

참고로, 관련 코드는 다음과 같다.

  • memcached_string_c_copy()에서 stringh length가 0인 경우 NULL을 리턴한다.
  • memcached_string_c_copy()에서 memory allocation 실패 시에 오류 처리하지 않고 NULL 리턴하는 것도 문제이다.
    • memcached_fetch_result() 성공하고 나서 value 주소를 리턴하기 위해 아래 함수가 호출된다.
    • 정상적으로 fetch한 결과인 데, NULL을 리턴하게 된다면 응용에서는 그 결과를 잘못 이해하게 된다.
char *memcached_string_c_copy(memcached_string_st *string)
{
  if (not memcached_string_length(string))
    return NULL;

  char *c_ptr= static_cast<char *>(libmemcached_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char)));

  if (not c_ptr)
    return NULL;

  memcpy(c_ptr, memcached_string_value(string), memcached_string_length(string));
  c_ptr[memcached_string_length(string)]= 0;

  return c_ptr;
}

char *memcached_string_take_value(memcached_string_st *self)
{
  assert_msg(self, "Invalid memcached_string_st");
  // If we fail at adding the null, we copy and move on
  if (memcached_success(memcached_string_append_null(self)))
  {
    return memcached_string_c_copy(self);
  }

  char *value= self->string;

  _init_string(self);

  return value;
}

@ing-eoking
Copy link
Collaborator

정상적으로 fetch한 결과인 데, NULL을 리턴하게 된다면 응용에서는 그 결과를 잘못 이해하게 된다.

libmemcached 문서에는 다음과 같이 쓰여 있습니다.

memcached_get() will return NULL on error. You must look at the value of error to determine what the actual error was.

memcached_get에서 에러 발생 유무는 NULL이 아닌 error 값을 확인하는 것을 권장하고 있습니다.

이러한 동작이 맞는 지를 검토한다.

정확히 어떤 동작이 맞는지 결정하기에는 어려울 것 같습니다.

memcached_get 에서 반환되는 value는 보통 동적으로 메모리를 할당해서 반환됩니다.
이 상태에서 value_length가 0인 value가 NULL이 아닌 경우,
오히려 응용에서는 value에 메모리가 할당되지 않았음에도 불구하고 free를 시도하게 될 수 있습니다.

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

2 participants