0002-buffer-Use-size_t-for-argc-argument-in-ssh_buffer_-u.patch 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. From 270d6aa2bb01f3430d07cce5f97b48b741e3df9c Mon Sep 17 00:00:00 2001
  2. From: Andreas Schneider <asn@cryptomilk.org>
  3. Date: Fri, 7 Dec 2018 12:06:03 +0100
  4. Subject: [PATCH] buffer: Use size_t for argc argument in ssh_buffer_(un)pack()
  5. Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
  6. Signed-off-by: Baruch Siach <baruch@tkos.co.il>
  7. ---
  8. Upstream status: commit c306a693f3fbe
  9. include/libssh/buffer.h | 4 ++--
  10. src/buffer.c | 38 +++++++++++++++++++-------------------
  11. 2 files changed, 21 insertions(+), 21 deletions(-)
  12. diff --git a/include/libssh/buffer.h b/include/libssh/buffer.h
  13. index 4721cbe06c20..1c375343ee14 100644
  14. --- a/include/libssh/buffer.h
  15. +++ b/include/libssh/buffer.h
  16. @@ -40,11 +40,11 @@ void *ssh_buffer_allocate(struct ssh_buffer_struct *buffer, uint32_t len);
  17. int ssh_buffer_allocate_size(struct ssh_buffer_struct *buffer, uint32_t len);
  18. int ssh_buffer_pack_va(struct ssh_buffer_struct *buffer,
  19. const char *format,
  20. - int argc,
  21. + size_t argc,
  22. va_list ap);
  23. int _ssh_buffer_pack(struct ssh_buffer_struct *buffer,
  24. const char *format,
  25. - int argc,
  26. + size_t argc,
  27. ...);
  28. #define ssh_buffer_pack(buffer, format, ...) \
  29. _ssh_buffer_pack((buffer), (format), __VA_NARG__(__VA_ARGS__), __VA_ARGS__, SSH_BUFFER_PACK_END)
  30. diff --git a/src/buffer.c b/src/buffer.c
  31. index b029f202660f..99863747fc3c 100644
  32. --- a/src/buffer.c
  33. +++ b/src/buffer.c
  34. @@ -809,7 +809,7 @@ ssh_buffer_get_ssh_string(struct ssh_buffer_struct *buffer)
  35. */
  36. static int ssh_buffer_pack_allocate_va(struct ssh_buffer_struct *buffer,
  37. const char *format,
  38. - int argc,
  39. + size_t argc,
  40. va_list ap)
  41. {
  42. const char *p = NULL;
  43. @@ -817,12 +817,12 @@ static int ssh_buffer_pack_allocate_va(struct ssh_buffer_struct *buffer,
  44. char *cstring = NULL;
  45. size_t needed_size = 0;
  46. size_t len;
  47. - int count; /* int for size comparison with argc */
  48. + size_t count;
  49. int rc = SSH_OK;
  50. for (p = format, count = 0; *p != '\0'; p++, count++) {
  51. /* Invalid number of arguments passed */
  52. - if (argc != -1 && count > argc) {
  53. + if (count > argc) {
  54. return SSH_ERROR;
  55. }
  56. @@ -881,7 +881,7 @@ static int ssh_buffer_pack_allocate_va(struct ssh_buffer_struct *buffer,
  57. }
  58. }
  59. - if (argc != -1 && argc != count) {
  60. + if (argc != count) {
  61. return SSH_ERROR;
  62. }
  63. @@ -891,11 +891,7 @@ static int ssh_buffer_pack_allocate_va(struct ssh_buffer_struct *buffer,
  64. */
  65. uint32_t canary = va_arg(ap, uint32_t);
  66. if (canary != SSH_BUFFER_PACK_END) {
  67. - if (argc == -1){
  68. - return SSH_ERROR;
  69. - } else {
  70. - abort();
  71. - }
  72. + abort();
  73. }
  74. }
  75. @@ -918,7 +914,7 @@ static int ssh_buffer_pack_allocate_va(struct ssh_buffer_struct *buffer,
  76. */
  77. int ssh_buffer_pack_va(struct ssh_buffer_struct *buffer,
  78. const char *format,
  79. - int argc,
  80. + size_t argc,
  81. va_list ap)
  82. {
  83. int rc = SSH_ERROR;
  84. @@ -934,11 +930,15 @@ int ssh_buffer_pack_va(struct ssh_buffer_struct *buffer,
  85. char *cstring;
  86. bignum b;
  87. size_t len;
  88. - int count; /* int for size comparison with argc */
  89. + size_t count;
  90. +
  91. + if (argc > 256) {
  92. + return SSH_ERROR;
  93. + }
  94. for (p = format, count = 0; *p != '\0'; p++, count++) {
  95. /* Invalid number of arguments passed */
  96. - if (argc != -1 && count > argc) {
  97. + if (count > argc) {
  98. return SSH_ERROR;
  99. }
  100. @@ -1010,7 +1010,7 @@ int ssh_buffer_pack_va(struct ssh_buffer_struct *buffer,
  101. }
  102. }
  103. - if (argc != -1 && argc != count) {
  104. + if (argc != count) {
  105. return SSH_ERROR;
  106. }
  107. @@ -1018,11 +1018,7 @@ int ssh_buffer_pack_va(struct ssh_buffer_struct *buffer,
  108. /* Check if our canary is intact, if not somthing really bad happened */
  109. uint32_t canary = va_arg(ap, uint32_t);
  110. if (canary != SSH_BUFFER_PACK_END) {
  111. - if (argc == -1){
  112. - return SSH_ERROR;
  113. - } else {
  114. - abort();
  115. - }
  116. + abort();
  117. }
  118. }
  119. return rc;
  120. @@ -1050,12 +1046,16 @@ int ssh_buffer_pack_va(struct ssh_buffer_struct *buffer,
  121. */
  122. int _ssh_buffer_pack(struct ssh_buffer_struct *buffer,
  123. const char *format,
  124. - int argc,
  125. + size_t argc,
  126. ...)
  127. {
  128. va_list ap;
  129. int rc;
  130. + if (argc > 256) {
  131. + return SSH_ERROR;
  132. + }
  133. +
  134. va_start(ap, argc);
  135. rc = ssh_buffer_pack_allocate_va(buffer, format, argc, ap);
  136. va_end(ap);
  137. --
  138. 2.20.1