Contact me: hankecnc@gmail.com

ultralcd.pde 中消息文本的空终止 #211

推推 grbl 3年前 (2023-02-05) 240次浏览
关闭
xyxxl 打开了这个问题 2012 年 8 月 12 日 · 3条评论
关闭

ultralcd.pde 中消息文本的空终止#211

xyxxl 打开了这个问题 2012 年 8 月 12 日 · 3条评论

评论

ultralcd.pde 中消息文本的空终止 #211

在 lcd_status()

strncpy(messagetext,message,LCD_WIDTH); //good
messaetext[strlen(message)] //bad you need to check the length of message before possibly writing into crazy land

probably should look like:
#DEFINE LCD_LAST (LCD_WIDTH) -1
void 
lcd_status(const char *message)
{
    strncpy(messagetext, message, LCD_LAST); //leave room for the null
    if (strlen(message) > LCD_LAST) {
        messagetext[LCD_LAST] = 0; //message at max or too large
    } else {
        messagetext[strlen(message)] = 0;//message smaller than buffer
    }

}

lcd_statuspgm can write past the end of messagetext if the message is too long....
coming out of the loop *target points to the fist byte beyond messagetext..
ran across some other stuff if you are interested..

ultralcd.pde 中消息文本的空终止 #211
作者

使
#define LCD_LAST((LCD_WIDTH)-1)

ultralcd.pde 中消息文本的空终止 #211

感谢您找到这个。

我总是对错误修复和改进感兴趣。

将它们提供给我的最佳方式是通过拉取请求。然后很容易与令人兴奋的代码合并。

在 2012 年 8 月 11 日下午 8:50,xyxxl 写道:

在 lcd_status()

strncpy(消息文本,消息,LCD_WIDTH);//good
messaetext[strlen(message)] //bad 你需要在可能写入疯狂的土地之前检查消息的长度

可能应该看起来像:
#DEFINE LCD_LAST (LCD_WIDTH) -1
void
lcd_status(const char *message)
{
strncpy(messagetext, message, LCD_LAST); //为空值留出空间
if (strlen(message) > LCD_LAST) {
messagetext[LCD_LAST] = 0; //消息最大或太大
} else {
messagetext[strlen(message)] = 0;//消息小于缓冲区
}

}

如果消息太长,lcd_statuspgm 可以写入超过消息文本的末尾….
从循环中出来 *target 指向消息文本之外的第一个字节..
如果你感兴趣的话会遇到其他一些东西..


直接回复此电子邮件或在 GitHub 上查看。