xpcom strings

Talk about code development, features, specific bugs, enhancements, patches, and similar things.
Forum rules
Please keep everything here strictly on-topic.
This board is meant for Pale Moon source code development related subjects only like code snippets, patches, specific bugs, git, the repositories, etc.

This is not for tech support! Please do not post tech support questions in the "Development" board!
Please make sure not to use this board for support questions. Please post issues with specific websites, extensions, etc. in the relevant boards for those topics.

Please keep things on-topic as this forum will be used for reference for Pale Moon development. Expect topics that aren't relevant as such to be moved or deleted.
nskpm

xpcom strings

Unread post by nskpm » 2020-07-01, 14:37

Hi all,

I work on xpcom component for Pale Moon on macOS, and I'm stuck on xpcom strings.

Here is code which demonstrates error I have in component:

Code: Select all

#include "mozilla-config.h"
#include "nsStringAPI.h"
#include <iostream>

using namespace std;

int main() {
        NS_NAMED_LITERAL_STRING(str, "test");
        cout << str.Length() << endl;
        cout << str.CharAt(0) << endl;
}
Line with Length() outputs 0, and segmentation fault happens on line with CharAt(). Instead of literal I tried nsString, nsEmbedString, nsAutoString, maybe, something else. The result is always the same.

I've seen similar code in Pale Moon sources, so, I can guess that code itself is correct, but something special should be done during compilation. But what exactly? Does anybody know?

User avatar
athenian200
Contributing developer
Contributing developer
Posts: 1537
Joined: 2018-10-28, 19:56
Location: Georgia

Re: xpcom strings

Unread post by athenian200 » 2020-07-01, 17:59

I think you need a check to make sure Length isn't 0 before passing off to CharAt? Actually, I think you want to make sure that CharAt is always a lower value than the length of the string in general. If the string's length is 0, then even being at position 0 is too high, because you'd need a string length of at least 1 (the size of a char) to have a character to display. In other words, you can't get any valid characters out of a zero-length string. There is no valid character position within it, because it has no characters. If you try to move to any position in a zero-length string, you're doing something invalid.

I don't know a lot about xpcom strings specifically, but that's how C/C++ works in my experience.
"The Athenians, however, represent the unity of these opposites; in them, mind or spirit has emerged from the Theban subjectivity without losing itself in the Spartan objectivity of ethical life. With the Athenians, the rights of the State and of the individual found as perfect a union as was possible at all at the level of the Greek spirit." -- Hegel's philosophy of Mind

vannilla
Moon Magic practitioner
Moon Magic practitioner
Posts: 2194
Joined: 2018-05-05, 13:29

Re: xpcom strings

Unread post by vannilla » 2020-07-01, 18:22

I'm not an expert of XPCOM either, but "NS_NAMED_LITERAL_STRING" currently expands to

Code: Select all

const nsDependentString n(reinterpret_cast<const nsAString::char_type*>(s), \
                          uint32_t((sizeof(s) / 2) - 1))
in which the second argument is supposed to be the length of the passed data, in your case "test".
Because this is C++ what is going on is definitely out of my knoweledge, but the fact that the string length is calculated with sizeof is still strange.

nskpm

Re: xpcom strings

Unread post by nskpm » 2020-07-02, 07:43

athenian200 wrote:
2020-07-01, 17:59
I think you need a check to make sure Length isn't 0 before passing off to CharAt?
Of course, it's clear that accessing char in string of length 0 will crash a program.

The problem is not that program crashes (this is just a consequence), the problem is that string is not initialized properly.

And the question is how to properly initialize it.