xpcom strings

Talk about code development, features, specific bugzilla bugs, enhancements, patches, and other highly technical 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 referenced Bugzilla bugs, mercurial, 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. Most "bug reports" do not belong in this board and should initially be posted in Community Support or other relevant support boards.

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.
Post Reply
User avatar
nskpm
New to the forum
New to the forum
Posts: 2
Joined: 2020-07-01, 14:17

xpcom strings

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: 365
Joined: 2018-10-28, 19:56
Location: Texas
Contact:

Re: xpcom strings

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 rising sun will eventually set
A newborn's life will fade.
From sun to moon, moon to sun...
Give peaceful rest to the living dead."

— The Legend of Zelda: Ocarina of Time

vannilla
Board Warrior
Board Warrior
Posts: 1138
Joined: 2018-05-05, 13:29

Re: xpcom strings

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.

User avatar
nskpm
New to the forum
New to the forum
Posts: 2
Joined: 2020-07-01, 14:17

Re: xpcom strings

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.

Post Reply