Uploaded image for project: 'WiredTiger'
  1. WiredTiger
  2. WT-9950

__wt_realloc with clear memory flag requires knowledge of old size

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • WT11.1.0, 6.2.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • 5
    • Storage Engines - 2022-10-17

      When wt_realloc is called with bytes_allocated_ret set to NULL, then it assumes that the bytes previous allocated is 0.  But this value is used to determine how much memory to clear if the "clear_memory" flag is on.  The effect is that if we call:

      __wt_realloc(session, NULL, 10, &p);
      strncpy(p, "hello", 6);
      __wt_realloc(session, NULL, 20, &p);

      then the bytes stored at "p" will be cleared.  __wt_realloc is a macro that calls __wt_realloc_func with clear_memory set to true.

      Currently _wt_realloc is hardly used directly, most often it is called via _wt_realloc_def, which requires the previous size to be stored.

      We should probably raise an error if bytes_allocated is NULL and clear_memory is on as an unsupported combination: the caller should either use __wt_realloc_noclear or pass the current size.

      There are a couple spots that this should trigger, that will need to be fixed:

      src/cursor/cur_json.c:    WT_RET(__wt_realloc(session, NULL, needed + 1, json_bufp));
      src/cursor/cur_stat.c:    WT_RET(__wt_realloc(session, NULL, len, &cst->desc_buf)); 

      I think these are the only places that call __wt_realloc directly with a NULL second argument.

            Assignee:
            monica.ng@mongodb.com Monica Ng
            Reporter:
            donald.anderson@mongodb.com Donald Anderson
            Ruby Chen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: