snprintf in nano-lib, when format %s with NULL, it have problem?

Asked by XuXiaodong on 2018-05-08

Hello,

In my project, when using snprintf with nano-lib, for example:

char buff[50];
char* expr; //expr is a variable used in my code, the pointer maybe NULL or a string

snprintf(buff, 50, "the string is %s",expr);

when expr is NULL, I know in newlib, the NULL will be transported to "NULL" string, but in nano-lib the snprintf (in nano-lib) cannot work. How Can I reslove the problem? thank you very much!

Question information

Language:
English Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
2018-05-08
Last reply:
2018-05-11
Leo HavmĂžller (leh-p) said : #1

> char* buff[50];
That is an array of 50 pointers?

> char* expr;
This is uninitialized memory (not null).

XuXiaodong (haleyxu) said : #2

Dear Leo,

    sorry for mistake writing, have fixed the typo in question.

   expr is a variable used in my code, the pointer maybe NULL or a string, now in NULL case, NULL is transformed to "NULL" string in newlib, but in nano-lib, it cannot work.

john (jkovach) said : #3

I don't think that printf is supposed to deal with null pointers, you shouldnt count on it. Besides, it's easy enough to write
expr ? expr : "NULL"

Can you help with this problem?

Provide an answer of your own, or ask XuXiaodong for more information if necessary.

To post a message you must log in.