[En-Nut-Discussion] NutThreadCreate - td_name safeguarding ?

Dave Smart Dave at Smart-Family.net
Thu Dec 11 13:49:32 CET 2003


NutOS 3.3.2.1:

I momentarily forgot the rules, and found a small problem.

I was calling NutThreadCreate with a rather long thread names as you see
here -
    NutThreadCreate("SoundThread", SoundThread, arg, 768);
    NutThreadCreate("PowerThread", PowerThread, arg, 768);

and when running a modification of the web server, I was getting string
runaway -
    37FC PowerThrGÍ7@ü4C?â ï¾Þ ï¾Þï¾Þï¾Þï¾Þd- 64 SLP 0CE2 3F43 37CD 721
OK
    34DF SoundThrû°4@ß1W?Ö ï¾Þ ï¾Þï¾Þï¾Þï¾Þ]UwòÎ̹¾=z¸tÓÔñžÿVËÚ·[ÀÜ){ç9
9n«zÛ÷]þ[²ú¹ßm»¯žW?oóÁ–Wwø±Þ~â©Fžr 64 SLP 0CD6 3F57 34B0 721 OK

sys/thread.h defines the size of td_name
    u_char  td_name[9];

Then in the NutThreadCreate function, it is safely copying based on the
sizeof the td_name
    ...
    memcpy(td->td_name, name, sizeof(td->td_name) - 1);

but only if I have /correctly/ defined a short name. May I suggest an
additional statement in NutThreadCreate -
    memcpy(td->td_name, name, sizeof(td->td_name) - 1);
    td->td_name[8] = '\0';	/* safeguard the name from runaway */

which I applied to my source and it does what you would expect -

    37F2 PowerThr 64 SLP 0CD8 3C1C 37C3 721 OK
    34D5 SoundThr 64 RDY 0A79 0000 34A6 721 OK

regards,
Dave Smart




More information about the En-Nut-Discussion mailing list