You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "xiaoxiang781216 (via GitHub)" <gi...@apache.org> on 2023/03/05 14:46:07 UTC
[GitHub] [nuttx] xiaoxiang781216 opened a new pull request, #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
xiaoxiang781216 opened a new pull request, #8726:
URL: https://github.com/apache/nuttx/pull/8726
## Summary
- fs/vfs: Add a new argument(size_t len) to inode_getpath
- libc: Add a new argument(size_t fulllen) to lib_getfullpath
- Replace all strcat with strlcat
- Replace all sprintf with snprintf
## Impact
Make the coe more safe
## Testing
CI
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#discussion_r1183568992
##########
arch/arm/src/tlsr82/tlsr82_flash_mtd.c:
##########
@@ -292,13 +292,16 @@ static void tlsr82_flash_print(const char *msg, const uint8_t *buf,
{
if (i % 16 == 0)
{
- off += sprintf(&print_buf[off], "0x%08x:", i);
+ snprintf(&print_buf[off],
+ sizeof(print_buf) - off, "0x%08x:", i);
+ off += strlen(&print_buf[off]);
}
- off += sprintf(&print_buf[off], "0x%02x ", buf[i]);
- i++;
+ snprintf(&print_buf[off],
+ sizeof(print_buf) - off, "0x%02x ", buf[i]);
+ off += strlen(&print_buf[off]);
- if (i % 16 == 0)
+ if (++i % 16 == 0)
Review Comment:
but the condition need be changed:
```
if (i % 16 == 15)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#discussion_r1183462717
##########
arch/arm/src/tlsr82/tlsr82_flash_mtd.c:
##########
@@ -292,13 +292,16 @@ static void tlsr82_flash_print(const char *msg, const uint8_t *buf,
{
if (i % 16 == 0)
{
- off += sprintf(&print_buf[off], "0x%08x:", i);
+ snprintf(&print_buf[off],
+ sizeof(print_buf) - off, "0x%08x:", i);
+ off += strlen(&print_buf[off]);
}
- off += sprintf(&print_buf[off], "0x%02x ", buf[i]);
- i++;
+ snprintf(&print_buf[off],
+ sizeof(print_buf) - off, "0x%02x ", buf[i]);
+ off += strlen(&print_buf[off]);
- if (i % 16 == 0)
+ if (++i % 16 == 0)
Review Comment:
Optional: As an alternative the `while (len--)` can be changed to
```
fo (i = 0; len-- > 0; i++)
{
...
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] xiaoxiang781216 commented on pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#issuecomment-1455281297
> @xiaoxiang781216 please merge that "Replace all strcat with strlcat" with the other "fs: ... strlcat"
@acassis it's different: the first replaces strcat with strlcat, the second replaces strncat with strlcat.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] jerpelea merged pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea merged PR #8726:
URL: https://github.com/apache/nuttx/pull/8726
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] xiaoxiang781216 commented on pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#issuecomment-1536674882
@jerpelea this patch replaces all well-known unsafe c function with the safe replacement, which should be understand by most developers.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#discussion_r1126337438
##########
drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.c:
##########
@@ -62,9 +62,10 @@ void bcmf_hexdump(FAR uint8_t *data, unsigned int len, unsigned long offset)
char_count = 0;
}
- sprintf(hex_line + 3 * char_count, "%02x ", data[i]);
- sprintf(char_line + char_count, "%c",
- data[i] < 0x20 || data[i] >= 0x7f? '.': data[i]);
+ snprintf(hex_line + 3 * char_count, sizeof(hex_line) - 3 * char_count,
+ "%02x ", data[i]);
+ snprintf(char_line + char_count, sizeof(char_line) - char_count,
+ "%c", data[i] < 0x20 || data[i] >= 0x7f? '.': data[i]);
Review Comment:
```suggestion
"%c", data[i] < 0x20 || data[i] >= 0x7f ? '.' : data[i]);
```
##########
arch/arm/src/phy62xx/uart.h:
##########
@@ -165,7 +165,11 @@ int hal_uart_set_tx_buf(UART_INDEX_e uart_index, uint8_t *buf,
uint16_t size);
int hal_uart_get_tx_ready(UART_INDEX_e uart_index);
int hal_uart_send_buff(UART_INDEX_e uart_index, uint8_t *buff, uint16_t len);
-#define logx(...) {char tmp_str[128]; sprintf(tmp_str, __VA_ARGS__); hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1);}
+#define logx(...) { \
+ char tmp_str[128]; \
+ snprintf(tmp_str, sizeof(tmp_str), __VA_ARGS__); \
+ hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1); \
+ }
Review Comment:
`do {} while (0)`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#discussion_r1183570394
##########
arch/arm/src/tlsr82/tlsr82_flash_mtd.c:
##########
@@ -292,13 +292,16 @@ static void tlsr82_flash_print(const char *msg, const uint8_t *buf,
{
if (i % 16 == 0)
{
- off += sprintf(&print_buf[off], "0x%08x:", i);
+ snprintf(&print_buf[off],
+ sizeof(print_buf) - off, "0x%08x:", i);
+ off += strlen(&print_buf[off]);
}
- off += sprintf(&print_buf[off], "0x%02x ", buf[i]);
- i++;
+ snprintf(&print_buf[off],
+ sizeof(print_buf) - off, "0x%02x ", buf[i]);
+ off += strlen(&print_buf[off]);
- if (i % 16 == 0)
+ if (++i % 16 == 0)
Review Comment:
yes, indeed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#discussion_r1183335121
##########
drivers/modem/alt1250/altcom_hdlr.c:
##########
@@ -2531,13 +2530,13 @@ static int32_t actpdn_pkt_compose(FAR void **arg,
out->iptype = apn->ip_type;
out->authtype = apn->auth_type;
- strncpy((FAR char *)out->apnname, (FAR const char *)apn->apn,
+ strlcpy((FAR char *)out->apnname, (FAR const char *)apn->apn,
sizeof(out->apnname));
if (apn->auth_type != LTE_APN_AUTHTYPE_NONE)
{
- strncpy((FAR char *)out->username, (FAR const char *)apn->user_name,
+ strlcpy((FAR char *)out->username, (FAR const char *)apn->user_name,
sizeof(out->username));
- strncpy((FAR char *)out->password, (FAR const char *)apn->password,
+ strlcpy((FAR char *)out->password, (FAR const char *)apn->password,
sizeof(out->password));
Review Comment:
Done.
##########
drivers/modem/alt1250/altcom_hdlr.c:
##########
@@ -2531,13 +2530,13 @@ static int32_t actpdn_pkt_compose(FAR void **arg,
out->iptype = apn->ip_type;
out->authtype = apn->auth_type;
- strncpy((FAR char *)out->apnname, (FAR const char *)apn->apn,
+ strlcpy((FAR char *)out->apnname, (FAR const char *)apn->apn,
sizeof(out->apnname));
if (apn->auth_type != LTE_APN_AUTHTYPE_NONE)
{
- strncpy((FAR char *)out->username, (FAR const char *)apn->user_name,
+ strlcpy((FAR char *)out->username, (FAR const char *)apn->user_name,
sizeof(out->username));
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#discussion_r1183336492
##########
arch/arm/src/phy62xx/uart.h:
##########
@@ -165,7 +165,12 @@ int hal_uart_set_tx_buf(UART_INDEX_e uart_index, uint8_t *buf,
uint16_t size);
int hal_uart_get_tx_ready(UART_INDEX_e uart_index);
int hal_uart_send_buff(UART_INDEX_e uart_index, uint8_t *buff, uint16_t len);
-#define logx(...) {char tmp_str[128]; sprintf(tmp_str, __VA_ARGS__); hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1);}
+#define logx(...) \
+ do { \
+ char tmp_str[128]; \
+ snprintf(tmp_str, sizeof(tmp_str), __VA_ARGS__); \
+ hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1); \
+ } while (0)
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] jerpelea commented on pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea commented on PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#issuecomment-1537917632
I agree with the change but a commit message will always be good
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] pkarashchenko commented on pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#issuecomment-1533627373
Please fix compilation issues
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#discussion_r1142239721
##########
arch/arm/src/phy62xx/uart.h:
##########
@@ -165,7 +165,12 @@ int hal_uart_set_tx_buf(UART_INDEX_e uart_index, uint8_t *buf,
uint16_t size);
int hal_uart_get_tx_ready(UART_INDEX_e uart_index);
int hal_uart_send_buff(UART_INDEX_e uart_index, uint8_t *buff, uint16_t len);
-#define logx(...) {char tmp_str[128]; sprintf(tmp_str, __VA_ARGS__); hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1);}
+#define logx(...) \
+ do { \
+ char tmp_str[128]; \
+ snprintf(tmp_str, sizeof(tmp_str), __VA_ARGS__); \
+ hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1); \
+ } while (0)
Review Comment:
```suggestion
do \
{ \
char tmp_str[128]; \
snprintf(tmp_str, sizeof(tmp_str), __VA_ARGS__); \
hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1); \
} while (0)
```
##########
drivers/modem/alt1250/altcom_hdlr.c:
##########
@@ -2531,13 +2530,13 @@ static int32_t actpdn_pkt_compose(FAR void **arg,
out->iptype = apn->ip_type;
out->authtype = apn->auth_type;
- strncpy((FAR char *)out->apnname, (FAR const char *)apn->apn,
+ strlcpy((FAR char *)out->apnname, (FAR const char *)apn->apn,
sizeof(out->apnname));
if (apn->auth_type != LTE_APN_AUTHTYPE_NONE)
{
- strncpy((FAR char *)out->username, (FAR const char *)apn->user_name,
+ strlcpy((FAR char *)out->username, (FAR const char *)apn->user_name,
sizeof(out->username));
Review Comment:
```suggestion
strlcpy((FAR char *)out->username, (FAR const char *)apn->user_name,
sizeof(out->username));
```
##########
drivers/modem/alt1250/altcom_hdlr.c:
##########
@@ -2531,13 +2530,13 @@ static int32_t actpdn_pkt_compose(FAR void **arg,
out->iptype = apn->ip_type;
out->authtype = apn->auth_type;
- strncpy((FAR char *)out->apnname, (FAR const char *)apn->apn,
+ strlcpy((FAR char *)out->apnname, (FAR const char *)apn->apn,
sizeof(out->apnname));
if (apn->auth_type != LTE_APN_AUTHTYPE_NONE)
{
- strncpy((FAR char *)out->username, (FAR const char *)apn->user_name,
+ strlcpy((FAR char *)out->username, (FAR const char *)apn->user_name,
sizeof(out->username));
- strncpy((FAR char *)out->password, (FAR const char *)apn->password,
+ strlcpy((FAR char *)out->password, (FAR const char *)apn->password,
sizeof(out->password));
Review Comment:
```suggestion
strlcpy((FAR char *)out->password, (FAR const char *)apn->password,
sizeof(out->password));
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] acassis commented on pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#issuecomment-1455227885
@xiaoxiang781216 please merge that "Replace all strcat with strlcat" with the other "fs: ... strlcat"
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#discussion_r1133039538
##########
arch/arm/src/phy62xx/uart.h:
##########
@@ -165,7 +165,11 @@ int hal_uart_set_tx_buf(UART_INDEX_e uart_index, uint8_t *buf,
uint16_t size);
int hal_uart_get_tx_ready(UART_INDEX_e uart_index);
int hal_uart_send_buff(UART_INDEX_e uart_index, uint8_t *buff, uint16_t len);
-#define logx(...) {char tmp_str[128]; sprintf(tmp_str, __VA_ARGS__); hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1);}
+#define logx(...) { \
+ char tmp_str[128]; \
+ snprintf(tmp_str, sizeof(tmp_str), __VA_ARGS__); \
+ hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1); \
+ }
Review Comment:
Done.
##########
drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.c:
##########
@@ -62,9 +62,10 @@ void bcmf_hexdump(FAR uint8_t *data, unsigned int len, unsigned long offset)
char_count = 0;
}
- sprintf(hex_line + 3 * char_count, "%02x ", data[i]);
- sprintf(char_line + char_count, "%c",
- data[i] < 0x20 || data[i] >= 0x7f? '.': data[i]);
+ snprintf(hex_line + 3 * char_count, sizeof(hex_line) - 3 * char_count,
+ "%02x ", data[i]);
+ snprintf(char_line + char_count, sizeof(char_line) - char_count,
+ "%c", data[i] < 0x20 || data[i] >= 0x7f? '.': data[i]);
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [nuttx] xiaoxiang781216 commented on pull request #8726: Replace the unsafe function(strcat, sprintf) with the safe one(strlcat, snprintf)
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #8726:
URL: https://github.com/apache/nuttx/pull/8726#issuecomment-1534003170
> Please fix compilation issues
Done, the origin code has bug which may make the buffer overflow, it is cached by compiler after we switch sprintf to snprintf.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org