0

I'm having a strange problem with Ncurses in C that I can't seem to find documented anywhere, nor on the web. Maybe I'm not using the correct search terms?

Here is the problem:

I have several windows and panels and LOTs of code. It all works perfectly UNTIL this happens:

mvwprintw(windowptr[i], y, x, NULL); <-- That NULL value is what sets off the bomb

I would expect that a NULL string would simply print a string of 0 length, but that's not what happens. What actually happens is that it corrupts all of my windows/panels. It seems to be somehow deleting all previously written chars and disabling the displaying of new chars on all windows / panels except the current window / panel. The current window / panel does not get blanked, but no new chars can be printed to it.

Is this a bug? ... or maybe an undocumented feature?

Here's my Ncurses version:

$ dpkg -l | grep -i ncurse
ii  libncurses5:amd64                             6.0+20160213-1ubuntu1                           amd64        shared libraries for terminal handling
ii  libncurses5:i386                              6.0+20160213-1ubuntu1                           i386         shared libraries for terminal handling
ii  libncurses5-dev:amd64                         6.0+20160213-1ubuntu1                           amd64        developer's libraries for ncurses
ii  libncursesw5:amd64                            6.0+20160213-1ubuntu1                           amd64        shared libraries for terminal handling (wide character support)
ii  libncursesw5:i386                             6.0+20160213-1ubuntu1                           i386         shared libraries for terminal handling (wide character support)
ii  mtr-tiny                                      0.86-1ubuntu0.1                                 amd64        Full screen ncurses traceroute tool
ii  ncurses-base                                  6.0+20160213-1ubuntu1                           all          basic terminal type definitions
ii  ncurses-bin                                   6.0+20160213-1ubuntu1                           amd64        terminal-related programs and man pages
ii  ncurses-term                                  6.0+20160213-1ubuntu1                           all          additional terminal type definitions
Hakachukai
  • 90
  • 6
  • 3
    Also see the [`mvwprintw(3)` man page](https://linux.die.net/man/3/mvwprintw). You should probably use an empty format string in this case. Consider, you cannot `printf(NULL)`, so you should not `mvwprintw(..., NULL)`. `mvwprintw(..., "")` should side-step the landmine. (I can't find a Q&A about `printf(NULL)` to cite for you). – jww Dec 18 '19 at 13:39
  • Is "" as a NULL terminated string != NULL? said another way: ```"" == NULL``` <-- is this true? – Hakachukai Dec 18 '19 at 13:55
  • Sorry I should have specified that I using GNU C. I edited the post to reflect that. – Hakachukai Dec 18 '19 at 14:03
  • After some thought, I think I now understand. "" is a pointer to an address who's value is NULL. NULL is a pointer to address 0x0. The two are not the same. Thanks for the help! – Hakachukai Dec 18 '19 at 14:06
  • 3
    `"" == NULL` No. A `""` is a pointer that points to a readonly location with a single byte that contains zero. A `NULL` is a 0. They differ. – KamilCuk Dec 18 '19 at 14:08
  • 1
    It's undefined behavior (for instance [this comment](https://www.geeksforgeeks.org/g-fact-44-passing-null-to-printf-in-c/)). ncurses does a number of pointer checks, anyway, but could miss some useful ones. – Thomas Dickey Dec 18 '19 at 22:40

1 Answers1

0

Actually (there's no sample MCVE) to test in this question), ncurses does check for this null pointer and is expected to ignore the printw which would be called after the wmove part.

To see this, follow the source-code:

  • mvprintw is always a function call (due to the variable-length argument list)
  • vw_printw is the next step, which calls
  • _nc_printf_string, which sees the null-pointer for the format, and returns null,
  • making vw_printw ignore the call.
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105