0

I have the following code:

static Atom _NET_WM_NAME = XInternAtom( display, "_NET_WM_NAME", false );

unsigned char* wm_data = NULL;
Atom wm_type;
int wm_format;
unsigned long wm_nitems, wm_bytes;

std::string title;

int ret = XGetWindowProperty( display, window, _NET_WM_NAME, 0, 512,
    false, utf8_string, &wm_type, &wm_format, &wm_nitems, &wm_bytes, &wm_data);

if( ret == Success && wm_nitems != 0 && wm_data != NULL ) {
    title = (const char*)wm_data;  // [1]
}

The _NET_WM_NAME property is a UTF8_STRING, as per the spec

So, in my code above, [1] assumes that the received data will always be NULL terminated. The code as it is works, but the returned byte amount (wm_nitems) does not include the NULL terminator, it is always the same as the actual string length. This is what makes me uncertain.

So my questions are:

  1. Is guaranteed that a read of a UTF8_STRING property will always return the string INCLUDING the NULL terminator?
  2. If so, why is the NULL terminator not included as part of the wm_nitems count?
  3. Can somebody point where this is specified in the spec?
  4. And in particular, is the above snippet correct for reading the _NET_WM_NAME property?
LoPiTaL
  • 2,495
  • 16
  • 23

1 Answers1

2

XGetWindowProperty

"XGetWindowProperty() always allocates one extra byte in prop_return (even if the property is zero length) and sets it to zero so that simple properties consisting of characters do not have to be copied into yet another string before use."


wm_nitems

nitems_return: Returns the actual number of 8-bit, 16-bit, or 32-bit items stored in the prop_return data

Why should the null terminator count as data. It is the courtesy of XGetWindowProperty to automagically return a null terminated string (see above), but the nul character is not part of the actual data (e.g., it is absolutely possible to set the window name without an ending nul char).

To put it differently: strlen doesn't count the terminating null character either, right?


Another thing, C strings are null terminated character arrays. The symbol NULL is the null pointer and has a special meaning in the C environment (see: https://en.cppreference.com/w/c/types/NULL).

  • '\0' is of type char
  • NULL is of type void* (implementation-defined, could also be 0)
Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11
  • `wm_net_name_atom` -> typo while adapting the code to the web. Edited. – LoPiTaL Feb 22 '23 at 18:05
  • about the `null`s. Yes, I know. I am referring here to the `null` terminator character (`\0`) – LoPiTaL Feb 22 '23 at 18:06
  • I know, i just wanted to point it out. – Erdal Küçük Feb 22 '23 at 18:07
  • the first part of your answer addresses all my questions. `XGetWindowProperty` adds it. It seems to not be required by the spec, and since it is not part of the property it does not count as a property byte. I went to the spec, instead of reading the function's documentation :( – LoPiTaL Feb 22 '23 at 18:07
  • "I went to the spec" The spec for Xlib is the programming manual and it says that an extra byte is allocated. What other spec did you go to? – n. m. could be an AI Feb 22 '23 at 18:36