Example #1
0
void test_pointers() {
  bool foo = true;
  char lowerBuf[20];

  auto str = w_string::build(&foo);
  snprintf(
      lowerBuf, sizeof(lowerBuf), "0x%" PRIx64, (uint64_t)(uintptr_t)(&foo));
  ok(str.size() == strlen_uint32(lowerBuf),
     "reasonable seeming bool pointer len, got %" PRIu32
     " vs expected %" PRIu32,
     str.size(),
     strlen_uint32(lowerBuf));
  ok(str.size() == strlen_uint32(str.c_str()),
     "string is really nul terminated, size %" PRIu32
     " strlen of c_str %" PRIu32,
     str.size(),
     strlen_uint32(str.c_str()));
  ok(!strcmp(lowerBuf, str.c_str()),
     "bool pointer rendered right hex value sprintf->%s, str->%s",
     lowerBuf,
     str.c_str());

  str = w_string::build(nullptr);
  ok(str.size() > 0, "nullptr has reasonable size: %" PRIsize_t, str.size());
  ok(str == w_string("0x0"), "nullptr looks right %s", str.c_str());

  void* zero = 0;
  ok(w_string::build(zero) == "0x0", "zero pointer looks right");
}
Example #2
0
static w_query_expr *suffix_parser(w_query *query, json_t *term)
{
  const char *ignore, *suffix;
  char *arg;
  w_string_t *str;
  int i, l;

  if (json_unpack(term, "[s,s]", &ignore, &suffix) != 0) {
    query->errmsg = strdup("must use [\"suffix\", \"suffixstring\"]");
    return NULL;
  }

  arg = strdup(suffix);
  if (!arg) {
    query->errmsg = strdup("out of memory");
    return NULL;
  }

  l = strlen_uint32(arg);
  for (i = 0; i < l; i++) {
    arg[i] = (char)tolower((uint8_t)arg[i]);
  }

  str = w_string_new_typed(arg, W_STRING_BYTE);
  free(arg);
  if (!str) {
    query->errmsg = strdup("out of memory");
    return NULL;
  }

  return w_query_expr_new(eval_suffix, dispose_suffix, str);
}
Example #3
0
void test_strings() {
  {
    auto hello = w_string::build("hello");
    ok(hello == w_string("hello"), "hello");
    ok(hello.size() == 5, "there are 5 chars in hello");
    ok(!strcmp("hello", hello.c_str()),
       "looks nul terminated `%s` %" PRIu32,
       hello.c_str(),
       strlen_uint32(hello.c_str()));
  }

  {
    w_string_piece piece("hello");
    ok(piece.size() == 5, "piece has 5 char size");
    auto hello = w_string::build(piece);
    ok(hello.size() == 5, "hello has 5 char size");
    ok(!strcmp("hello", hello.c_str()), "looks nul terminated");
  }

  {
    char foo[] = "foo";
    auto str = w_string::build(foo);
    ok(str.size() == 3, "foo has 3 char size");
    ok(!strcmp("foo", foo), "foo matches");
  }
}
Example #4
0
// Load up the words data file and build a list of strings from that list.
// Each of those strings is prefixed with the supplied string.
// If there are fewer than limit entries available in the data file, we will
// abort.
std::vector<w_string> build_list_with_prefix(const char* prefix, size_t limit) {
  std::vector<w_string> strings;
  char buf[512];
  FILE *f = fopen("thirdparty/libart/tests/words.txt", "r");

#ifdef WATCHMAN_TEST_SRC_DIR
  if (!f) {
    f = fopen(WATCHMAN_TEST_SRC_DIR "/thirdparty/libart/tests/words.txt", "r");
  }
#endif

  if (!f) {
    f = fopen("watchman/thirdparty/libart/tests/words.txt", "r");
  }

  while (fgets(buf, sizeof buf, f)) {
    // Remove newline
    uint32_t len = strlen_uint32(buf);
    buf[len - 1] = '\0';
    strings.emplace_back(w_string::printf("%s%s", prefix, buf));

    if (strings.size() >= limit) {
      break;
    }
  }

  if (strings.size() < limit) {
    abort();
  }

  return strings;
}
Example #5
0
void run_correctness_test(
    struct watchman_ignore* state,
    const struct test_case* tests,
    uint32_t num_tests) {
  uint32_t i;

  for (i = 0; i < num_tests; i++) {
    bool res = state->isIgnored(tests[i].path, strlen_uint32(tests[i].path));
    ok(res == tests[i].ignored, "%s expected=%d actual=%d", tests[i].path,
       tests[i].ignored, res);
  }
}
Example #6
0
static void XMLCALL expat_xml_decl_cb(
      void *data,
      const char *v,
      const char *e,
      int s)
{
  xmlGParser *xml_parser;
  xmlGContext *xml_ctxt ;
  xmlGFilterChain *filter_chain ;
  xmlGParserCommon *c ;
  unsigned int v_len = 0, e_len = 0 ;

  XMLGASSERT(data != NULL, "data is NULL");
  xml_parser = data;
  XMLGASSERT(xml_parser != NULL, "xml_parser is NULL");

  c = xmlg_get_parser_common(xml_parser);
  xml_ctxt = c->xml_ctxt;
  filter_chain = c->filter_chain ;

  /* If there is no filter chain, there is nothing to do. */
  if (filter_chain == NULL)
    return ;

  /* If v is NULL, its a text declaration according to expat
     documentation. */
  if (v == NULL)
    return ;

  v_len = strlen_uint32(v) ;
  if (e != NULL)
    e_len = strlen_uint32(e) ;

  if (! xmlg_fc_execute_xml_decl(filter_chain,
                    (const unsigned char *)v, v_len,
                    (const unsigned char *)e, e_len, s)) {
    xmlg_parser_error_abort(xml_parser, FALSE, NULL, 0) ;
  }
}
Example #7
0
static void XMLCALL expat_xml_dtd_cb(
      void  *data,
      const char *n,
      const char *s,
      const char *p,
      int h)
{
  xmlGParser *xml_parser;
  xmlGContext *xml_ctxt ;
  xmlGFilterChain *filter_chain ;
  xmlGParserCommon *c ;
  unsigned int n_len, s_len = 0, p_len = 0 ;

  XMLGASSERT(data != NULL, "data is NULL");
  xml_parser = data;
  XMLGASSERT(xml_parser != NULL, "xml_parser is NULL");

  c = xmlg_get_parser_common(xml_parser);
  xml_ctxt = c->xml_ctxt;
  filter_chain = c->filter_chain ;

  /* If there is no filter chain, there is nothing to do. */
  if (filter_chain == NULL)
    return ;

  n_len = strlen_uint32(n);
  if (s != NULL)
    s_len = strlen_uint32(s);
  if (p != NULL)
    p_len = strlen_uint32(p);

  if (! xmlg_fc_execute_start_dtd(filter_chain,
                                (const unsigned char *)n, n_len,
                                (const unsigned char *)s, s_len,
                                (const unsigned char *)p, p_len,
                                h)) {
    xmlg_parser_error_abort(xml_parser, FALSE, NULL, 0) ;
  }
}
Example #8
0
static void parse_error_cb(
      xmlGParser *xml_parser,
      hqn_uri_t *uri,
      uint32 line,
      uint32 column,
      uint8 *detail,
      int32  detail_len)
{
  XMLExecContext *p_xmlexec_context ;
  uint8 *uri_name ;
  uint32 uri_name_len ;

  HQASSERT(xml_parser != NULL, "xml_parser is NULL") ;

  UNUSED_PARAM(xmlGParser*, xml_parser);

  p_xmlexec_context = SLL_GET_HEAD(&xml_context.sls_contexts, XMLExecContext, sll) ;

  /* Because we go recursive on parse instances, protect against
     raising a PS error more than once. */
  /** \todo if xmlexec goes recursive, we will raise a PS error more than
     once. */
  if (p_xmlexec_context->error_occured)
    return ;

  /* Although this should never happen, continue to report at least
     some error rather than no error at all. */
  if (! hqn_uri_get_field(uri, &uri_name, &uri_name_len, HQN_URI_ENTIRE)) {
    uri_name = (uint8 *)"Invalid URI while handling error condition." ;
    uri_name_len = strlen_uint32((const char *)uri_name) ;
  }

  /* We set the default parse error callback to a generic form so that
     we do not display a PDL specfic error message when we don't yet
     know the PDL type. */
#define SHORTFORM ("URI: %.*s; Line: %d; Column: %d.")
#define LONGFORM ("URI: %.*s; Line: %d; Column: %d; XMLInfo: %.*s.")

  /* libgenxml internal errors may as well come out as UNDEFINED PS
     errors */
  if (detail == NULL || detail_len == 0) {
    (void)detailf_error_handler(UNDEFINED, SHORTFORM, uri_name_len, uri_name, line,
                                column) ;
  } else {
    (void)detailf_error_handler(UNDEFINED, LONGFORM, uri_name_len, uri_name, line,
                                column, detail_len, detail) ;
  }
}
Example #9
0
Bool psdev_uri_from_open_file(
      FILELIST* flptr,
      hqn_uri_t **psdev_abs_uri)
{
  hqn_uri_t *new_uri ;
  uint32 devicelen, clistlen, buf_len ;
  uint8 *insert_point ;
  uint8 buf[ PSDEV_SCHEME_NAME_LEN + LONGESTFILENAME +
             LONGESTDEVICENAME + 1 ] ;

  HQASSERT(flptr != NULL, "flptr is NULL") ;
  HQASSERT(psdev_abs_uri != NULL, "psdev_abs_uri is NULL") ;

  /* Expecting a file, not a filter. */
  if ( isIFilter(flptr) || !flptr->device )
    return error_handler(UNDEFINED) ;

  devicelen = strlen_uint32((char *)flptr->device->name) ;
  clistlen = flptr->len ; /* clist not always null terminated */

  insert_point = buf ;
  buf_len = PSDEV_SCHEME_NAME_LEN + devicelen + clistlen ;
  if (clistlen > 0 && flptr->clist[0] != '/')
    buf_len++ ;

  HqMemCpy(insert_point, psdev_scheme_name, PSDEV_SCHEME_NAME_LEN) ;
  insert_point += PSDEV_SCHEME_NAME_LEN ;
  HqMemCpy(insert_point, flptr->device->name, devicelen) ;
  insert_point += devicelen ;
  if (clistlen > 0 && flptr->clist[0] != '/')
    *(insert_point++) = '/' ;
  HqMemCpy(insert_point, flptr->clist, clistlen) ;
  insert_point += clistlen ;

  if (! hqn_uri_parse(core_uri_context,
                      &new_uri,
                      buf,
                      buf_len,
                      TRUE /* copy string */)) {
    return error_handler(TYPECHECK) ;
  }

  *psdev_abs_uri = new_uri ;

  return TRUE ;
}
Example #10
0
Bool psdev_base_uri_from_open_file(
      FILELIST* flptr,
      hqn_uri_t **psdev_base_uri)
{
  hqn_uri_t *new_uri ;
  uint32 devicelen, buf_len ;
  uint8 *insert_point ;
  /* Allow for a / on the end */
  static uint8 buf[ PSDEV_SCHEME_NAME_LEN + LONGESTDEVICENAME + 1 ] ;
  static uint8 *rsd_scheme_name = (uint8*)"rsd://" ;

#define RSD_SCHEME_NAME_LEN (sizeof(rsd_scheme_name) - 1)

  HQASSERT(flptr != NULL, "flptr is NULL") ;
  HQASSERT(psdev_base_uri != NULL, "psdev_base_uri is NULL") ;

  insert_point = buf ;

  if (isIRSDFilter(flptr)) {
    buf_len = RSD_SCHEME_NAME_LEN ;
    HqMemCpy(insert_point, rsd_scheme_name, RSD_SCHEME_NAME_LEN) ;
  } else {
    devicelen = strlen_uint32((char *)flptr->device->name) ;
    buf_len = PSDEV_SCHEME_NAME_LEN + devicelen ;
    HqMemCpy(insert_point, psdev_scheme_name, PSDEV_SCHEME_NAME_LEN) ;
    insert_point += PSDEV_SCHEME_NAME_LEN ;
    HqMemCpy(insert_point, flptr->device->name, devicelen) ;
  }

  if (! hqn_uri_parse(core_uri_context,
                      &new_uri,
                      buf,
                      buf_len,
                      TRUE /* copy the string */)) {
    return error_handler(TYPECHECK) ;
  }

  *psdev_base_uri = new_uri ;

  return TRUE ;
}
Example #11
0
/* make a lowercased copy of string */
w_string_t *w_string_new_lower_typed(const char *str,
    w_string_type_t type)
{
  w_string_t *s;
  uint32_t len = strlen_uint32(str);
  char *buf;
  uint32_t i;

  s = (w_string_t*)(new char[sizeof(*s) + len + 1]);
  new (s) watchman_string();

  s->refcnt = 1;
  s->len = len;
  buf = (char*)(s + 1);
  // TODO: optionally use ICU
  for (i = 0; i < len; i++) {
    buf[i] = (char)tolower((uint8_t)str[i]);
  }
  buf[len] = 0;
  s->buf = buf;
  s->type = type;

  return s;
}
Example #12
0
bool w_is_path_absolute_cstr(const char *path) {
  return w_is_path_absolute_cstr_len(path, strlen_uint32(path));
}
Example #13
0
bool w_string_equal_cstring(const w_string_t *a, const char *b)
{
  uint32_t blen = strlen_uint32(b);
  if (a->len != blen) return false;
  return memcmp(a->buf, b, a->len) == 0 ? true : false;
}
Example #14
0
w_string_t *w_string_new_typed(const char *str, w_string_type_t type) {
  return w_string_new_len_typed(str, strlen_uint32(str), type);
}
Example #15
0
w_string::w_string(const char* buf, w_string_type_t stringType)
    : w_string(
          w_string_new_len_typed(buf, strlen_uint32(buf), stringType),
          false) {}
Example #16
0
/* ============================================================================
 * Native expat callbacks.
 */
static
void XMLCALL expat_start_namespace_cb(
      void *data,
      const char *prefix,
      const char *uri)
{
  xmlGParser *xml_parser;
  xmlGContext *xml_ctxt ;
  xmlGFilterChain *filter_chain ;
  xmlGParserCommon *c ;
  const xmlGIStr *istr_prefix, *istr_uri, *istr_alias_uri ;
  uint32 prefixlen, urilen ;


  XMLGASSERT(data != NULL, "data is NULL");
  xml_parser = data;
  XMLGASSERT(xml_parser != NULL, "xml_parser is NULL");

  c = xmlg_get_parser_common(xml_parser);
  xml_ctxt = c->xml_ctxt;
  filter_chain = c->filter_chain ;

  /* If there is no filter chain, there is nothing to do. */
  if (filter_chain == NULL)
    return ;

  prefixlen = 0 ;
  urilen = 0 ;
  istr_prefix = NULL ;
  istr_uri = NULL ;

  if (prefix != NULL) {
    prefixlen = strlen_uint32(prefix) ;
    if (! xmlg_istring_create(xml_ctxt, &istr_prefix, (uint8 *)prefix, prefixlen)) {
      goto error ;
    }
  }
  if (uri != NULL) {
    urilen = strlen_uint32(uri) ;
    if (! xmlg_istring_create(xml_ctxt, &istr_alias_uri, (uint8 *)uri, urilen)) {
      goto error ;
    }

    if (c->mapuri_cb == NULL) {
      istr_uri = istr_alias_uri ;
    } else {
      if (! c->mapuri_cb(istr_alias_uri, &istr_uri)) {
        goto error ;
      }
    }
    HQASSERT(istr_uri != NULL, "istr_uri pointer is NULL") ;
  }

  if (! xmlg_fc_execute_namespace(filter_chain, istr_prefix, istr_uri)) {
    xmlg_parser_error_abort(xml_parser, FALSE, NULL, 0) ;
  }

  goto done ;

 error:
  xmlg_parser_error_abort(xml_parser, TRUE, NULL, 0) ;

 done:
  /* Destroy the strings. */
  xmlg_istring_destroy(xml_ctxt, &istr_prefix) ;
  xmlg_istring_destroy(xml_ctxt, &istr_uri) ;

  return;
}
Example #17
0
w_string_t *w_string_path_cat_cstr(w_string_t *parent, const char *rhs) {
  return w_string_path_cat_cstr_len(parent, rhs, strlen_uint32(rhs));
}