Photon 2 does not tolerate Log.info() with a NULL value

Hey,

This works fine on Argons and Borons when the variable command is NULL:

Log.info("test: %s", command);

But it does not work well on Photon 2 (5.8.2 and potentially other deviceOS versions).

AI was quick to flag this as a potential red flag:

Logging Pointers: Ensure that Log.info can handle NULL
pointers gracefully, as logging NULL might cause issues
in some logging implementations.

Of course, these issues can (and should) be avoided in code, but I realize now that I was relying too much on the leniency of Log.info() in handling NULL pointers.

Lesson learned!


How to reproduce:

  char args[] = "test 123";
  Log.info("Command received: %s", args);
  char *command = strtok((char *)args, " ");
  Log.info("Command first part received: %s", command);
  command = strtok(NULL, " ");
  Log.info("Command second part received: %s", command);

  char args2[] = "test";
  Log.info("Command received: %s", args2);
  char *command2 = strtok((char *)args2, " ");
  Log.info("Command first part received: %s", command2);
  command2 = strtok(NULL, " ");

  // this crashes on a Photon 2 since command2 is NULL at this point
  Log.info("Command second part received: %s", command2);

2 Likes

That's slightly unexpected. Log.info just uses vsnprintf internally. And that should come from the newlib standard C library that we use in Device OS, which is platform independent. My guess is that it just happens to work on nRF52 because of what happens to be at address 0, but I don't know that for sure.

1 Like

Thanks for the insight.
If you think it’s worth warning folks about this, it could be added to the migration guides.

In the past we were able to modify the docs ourselves (via a PR).
Is this still the case? I couldn't find how.
Thanks