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

feat(tianmu): fix invalid write/read issue. (#817) #904

Merged
merged 2 commits into from
Nov 10, 2022

Conversation

lujiashun
Copy link

Summary about this PR

Issue Number: ref #817

Tests Check List

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Changelog

  • New Feature
  • Bug Fix
  • Improvement
  • Performance Improvement
  • Build/Testing/CI/CD
  • Documentation
  • Not for changelog (changelog entry is not required)

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features

@mergify
Copy link
Contributor

mergify bot commented Nov 8, 2022

Thanks for the contribution!
I have applied any labels matching special text in your PR Changelog.

Please review the labels and make any necessary changes.

@mergify mergify bot added the PR-feature feature for pull request label Nov 8, 2022
@lujiashun lujiashun self-assigned this Nov 8, 2022
@lujiashun
Copy link
Author

lujiashun commented Nov 9, 2022

#include <cassert>
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
using namespace std;
void  myprintf(char const *owner_name, char const *object_id, int _default_block_size) {
  char *file_name_ = nullptr;
  size_t filename_n_position_ = 0;
  string tianmu_sysvar_cachefolder = "/data/sssfassdgbf";
    // read the configuration parameter
  constexpr size_t MIN_OWN_NAME_LEN = 3;
  constexpr size_t OWNER_AND_OJBECT_LEN = 6;
  constexpr size_t NUMBER_FILL_LEN = 6;
  constexpr size_t RANDOM_LEN = 8;
  constexpr size_t OBJECT_PTR_LEN = 8;
  constexpr size_t SUFFIX_LEN = 11;
  size_t filename_offset = 0;

  // read the configuration parameter
  std::string temp_filename = tianmu_sysvar_cachefolder;
  char last_char = temp_filename[temp_filename.size()-1];
  if (last_char != '/' && last_char != '\\') {
                temp_filename += "/";
  }

  // copy the temporary folder first
  // "...path.../XXXXXXnnnnnnAAAAAAAABBBBBBBB.tianmu_tmp"
  size_t total_length = temp_filename.length() + OWNER_AND_OJBECT_LEN + NUMBER_FILL_LEN + RANDOM_LEN + OBJECT_PTR_LEN + SUFFIX_LEN + 1;
  file_name_ = new char[total_length];
  file_name_[total_length -1] = 0;
  std::strcpy(file_name_, temp_filename.c_str());
  filename_offset = temp_filename.length();

  // fill the file name
  int i = 0, j = 0;

  while (owner_name[j] != 0 && i < OWNER_AND_OJBECT_LEN) file_name_[filename_offset + (i++)] = owner_name[j++];
  while (i < MIN_OWN_NAME_LEN) file_name_[filename_offset + (i++)] = '_';
  j = 0;
  while (object_id[j] != 0 && i < OWNER_AND_OJBECT_LEN) file_name_[filename_offset  + (i++)] = object_id[j++];
  while (i < OWNER_AND_OJBECT_LEN) file_name_[filename_offset + (i++)] = '_';
  filename_offset += OWNER_AND_OJBECT_LEN;

  filename_n_position_ = filename_offset;
  std::cout << filename_n_position_ <<std::endl;
  snprintf(file_name_ + filename_offset,NUMBER_FILL_LEN + 1,"%s", "000000" );
  filename_offset += NUMBER_FILL_LEN;

  char buf[30]={};
  unsigned int random_number = 0;
  random_number |= ((rand() % 1024) << 21);
  random_number |= ((rand() % 1024) << 11);
  random_number |= (rand() % 2048);
  std::snprintf(file_name_ + filename_offset,RANDOM_LEN + 1,"%08X", random_number );
  filename_offset += RANDOM_LEN;


  std::snprintf(buf,sizeof(buf), "%p", &random_number);
  if (std::strlen(buf) >= OBJECT_PTR_LEN)
    std::memcpy(file_name_+ filename_offset,buf,OBJECT_PTR_LEN);
  else {
    std::strcpy(file_name_ +filename_offset + (OBJECT_PTR_LEN - std::strlen(buf)), buf);
    std::memset(file_name_ + filename_offset, '0', OBJECT_PTR_LEN - std::strlen(buf));
  }
  filename_offset += OBJECT_PTR_LEN;
  std::cout << buf <<std::endl;

  std::snprintf(file_name_ + filename_offset,SUFFIX_LEN + 1,"%s", ".tianmu_tmp");
  filename_offset += SUFFIX_LEN;

  assert(file_name_[filename_offset] == 0);
  std::cout << file_name_ <<std::endl;
  std::cout << file_name_[filename_n_position_] << std::endl;

}
int main(int argc ,char** argv)
{
   myprintf("lj","ob",20);
}

@lujiashun lujiashun marked this pull request as draft November 9, 2022 06:40
@lujiashun
Copy link
Author

root@ub01:/data# ./a.out
24
0x7fff1ac45194
/data/sssfassdgbf/lj_ob_0000002CFE30690x7fff1a.tianmu_tmp
0

@lujiashun lujiashun force-pushed the feat-817-stonedb57 branch 2 times, most recently from e3d4be4 to 925401c Compare November 9, 2022 07:19
@lujiashun lujiashun marked this pull request as ready for review November 9, 2022 07:58
Copy link
Contributor

@RingsC RingsC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hustjieke
Copy link
Collaborator

Fix the same problem commentted in:
#902

Copy link
Collaborator

@hustjieke hustjieke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mergify mergify bot merged commit 907910d into stoneatom:stonedb-5.7-dev Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR-feature feature for pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants