1

I'm using the libxslt C library, and need to pass in parameters as const char *. I'm wrapping the library in a Qt C++ class, so the parameters are stored in the C++ class are store as QMap<QString, QString>.

My first attempt was simply:

const char *params[32];
int index = 0;
if (m_params.size() > 0) {
    QMapIterator<QString, QString> it(m_params);
    while (it.hasNext()) {
        it.next();

        params[index++] = it.key().toLocal8Bit().data();

        params[index++] = it.value().toLocal8Bit().data();
    }
}

params[index++] = nullptr;

qDebug() << params[0] << params[1]; // 0 0

But I realise that this isn't working because the QByteArray from toLocal8bit goes out of scope almost as soon as I've used it.

I've tried using strcpy - but have the same scope issues:

m_params.insert("some-key", "some-value", "another-key", "another-value");

if (m_params.size() > 0) {
    QMapIterator<QString, QString> it(m_params);
    while (it.hasNext()) {
        it.next();

        char buffer[32];

        strcpy(buffer, it.key().toLocal8Bit().data());
        params[index++] = buffer;

        strcpy(buffer, it.value().toLocal8Bit().data());
        params[index++] = buffer;
    }
}

params[index++] = nullptr;

qDebug() << params[0] << params[1]; // another-value another-value

So now I have a list of params all with the same value.

When I set all the values manually, I get the expected outcomes:

const char *params[32];
int index = 0;

params[index++] = "something";
params[index++] = "something-else";

params[index++] = nullptr;

qDebug() << params[0] << params[1]; // something something-else
HorusKol
  • 8,375
  • 10
  • 51
  • 92

1 Answers1

0

This is easy enough - you need to ensure that the buffer for the parameters persists long enough. Do not use fixed-size arrays - you're setting yourself up for a buffer overflow.

class Params {
  QByteArray buf;
  QVector<const char *> params;
public:
  Params() = default;
  template <class T> explicit Params(const T& map) {
    QVector<int> indices;
    indices.reserve(map.size());
    params.reserve(map.size()+1);
    for (auto it = map.begin(); it != map.end(); ++it) {
      indices.push_back(buf.size());
      buf.append(it.key().toLocal8Bit());
      buf.append('\0');
      indices.push_back(buf.size());
      buf.append(it.value().toLocal8Bit());
      buf.append('\0');
    }
    for (int index : qAsConst(indices))
      params.push_back(buf.constData() + index);
    params.push_back(nullptr);
  }
  operator const char **() const { return const_cast<const char**>(params.data()); }
  operator const char *const*() const { return params.data(); }
  operator QVector<const char*>() const { return params; }
};

void MyClass::method() const {
  Params params{m_params};
  ...
  res = xsltApplyStylesheet(cur, doc, params);
  ...
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313