diff mbox series

[FFmpeg-devel,01/10] avformat/utils: Make ff_data_to_hex() zero-terminate the string

Message ID AM7PR03MB6660E7B5CE9FC57743881E148F6D9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit e38eaf47491a864b2180d1ade87ed0ef39dd6d34
Headers show
Series [FFmpeg-devel,01/10] avformat/utils: Make ff_data_to_hex() zero-terminate the string | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 6, 2021, 1:08 a.m. UTC
Most callers want it that way anyway.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/hls.c       |  2 --
 libavformat/hlsenc.c    |  1 -
 libavformat/httpauth.c  |  5 -----
 libavformat/internal.h  | 11 +++++++++++
 libavformat/movenc.c    |  1 -
 libavformat/omadec.c    |  1 -
 libavformat/rtmpproto.c |  2 --
 libavformat/sdp.c       |  3 ---
 libavformat/takdec.c    |  1 -
 libavformat/utils.c     |  1 +
 10 files changed, 12 insertions(+), 16 deletions(-)

Comments

Andreas Rheinhardt Dec. 7, 2021, 11:32 a.m. UTC | #1
Andreas Rheinhardt:
> Most callers want it that way anyway.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavformat/hls.c       |  2 --
>  libavformat/hlsenc.c    |  1 -
>  libavformat/httpauth.c  |  5 -----
>  libavformat/internal.h  | 11 +++++++++++
>  libavformat/movenc.c    |  1 -
>  libavformat/omadec.c    |  1 -
>  libavformat/rtmpproto.c |  2 --
>  libavformat/sdp.c       |  3 ---
>  libavformat/takdec.c    |  1 -
>  libavformat/utils.c     |  1 +
>  10 files changed, 12 insertions(+), 16 deletions(-)
> 
> diff --git a/libavformat/hls.c b/libavformat/hls.c
> index 52a031ed54..557faf8e8d 100644
> --- a/libavformat/hls.c
> +++ b/libavformat/hls.c
> @@ -1284,7 +1284,6 @@ static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg,
>          char iv[33], key[33], url[MAX_URL_SIZE];
>          ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
>          ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
> -        iv[32] = key[32] = '\0';
>          if (strstr(seg->url, "://"))
>              snprintf(url, sizeof(url), "crypto+%s", seg->url);
>          else
> @@ -2074,7 +2073,6 @@ static int hls_read_header(AVFormatContext *s)
>              if (strstr(in_fmt->name, "mov")) {
>                  char key[33];
>                  ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
> -                key[32] = '\0';
>                  av_dict_set(&options, "decryption_key", key, AV_OPT_FLAG_DECODING_PARAM);
>              } else if (!c->crypto_ctx.aes_ctx) {
>                  c->crypto_ctx.aes_ctx = av_aes_alloc();
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index cfd0c036d1..1c2a556375 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -745,7 +745,6 @@ static int do_encrypt(AVFormatContext *s, VariantStream *vs)
>              memcpy(iv, hls->iv, sizeof(iv));
>          }
>          ff_data_to_hex(buf, iv, sizeof(iv), 0);
> -        buf[32] = '\0';
>          memcpy(hls->iv_string, buf, sizeof(hls->iv_string));
>      }
>  
> diff --git a/libavformat/httpauth.c b/libavformat/httpauth.c
> index 4f79c78edc..0a98ff80a5 100644
> --- a/libavformat/httpauth.c
> +++ b/libavformat/httpauth.c
> @@ -156,7 +156,6 @@ static char *make_digest_auth(HTTPAuthState *state, const char *username,
>      for (i = 0; i < 2; i++)
>          cnonce_buf[i] = av_get_random_seed();
>      ff_data_to_hex(cnonce, (const uint8_t*) cnonce_buf, sizeof(cnonce_buf), 1);
> -    cnonce[2*sizeof(cnonce_buf)] = 0;
>  
>      md5ctx = av_md5_alloc();
>      if (!md5ctx)
> @@ -166,7 +165,6 @@ static char *make_digest_auth(HTTPAuthState *state, const char *username,
>      update_md5_strings(md5ctx, username, ":", state->realm, ":", password, NULL);
>      av_md5_final(md5ctx, hash);
>      ff_data_to_hex(A1hash, hash, 16, 1);
> -    A1hash[32] = 0;
>  
>      if (!strcmp(digest->algorithm, "") || !strcmp(digest->algorithm, "MD5")) {
>      } else if (!strcmp(digest->algorithm, "MD5-sess")) {
> @@ -174,7 +172,6 @@ static char *make_digest_auth(HTTPAuthState *state, const char *username,
>          update_md5_strings(md5ctx, A1hash, ":", digest->nonce, ":", cnonce, NULL);
>          av_md5_final(md5ctx, hash);
>          ff_data_to_hex(A1hash, hash, 16, 1);
> -        A1hash[32] = 0;
>      } else {
>          /* Unsupported algorithm */
>          av_free(md5ctx);
> @@ -185,7 +182,6 @@ static char *make_digest_auth(HTTPAuthState *state, const char *username,
>      update_md5_strings(md5ctx, method, ":", uri, NULL);
>      av_md5_final(md5ctx, hash);
>      ff_data_to_hex(A2hash, hash, 16, 1);
> -    A2hash[32] = 0;
>  
>      av_md5_init(md5ctx);
>      update_md5_strings(md5ctx, A1hash, ":", digest->nonce, NULL);
> @@ -195,7 +191,6 @@ static char *make_digest_auth(HTTPAuthState *state, const char *username,
>      update_md5_strings(md5ctx, ":", A2hash, NULL);
>      av_md5_final(md5ctx, hash);
>      ff_data_to_hex(response, hash, 16, 1);
> -    response[32] = 0;
>  
>      av_free(md5ctx);
>  
> diff --git a/libavformat/internal.h b/libavformat/internal.h
> index 20e93d9267..f43e408548 100644
> --- a/libavformat/internal.h
> +++ b/libavformat/internal.h
> @@ -484,6 +484,17 @@ void ff_flush_packet_queue(AVFormatContext *s);
>   */
>  int ff_mkdir_p(const char *path);
>  
> +/**
> + * Write hexadecimal string corresponding to given binary data. The string
> + * is zero-terminated.
> + *
> + * @param buf       the output string is written here;
> + *                  needs to be at least 2 * size + 1 bytes long.
> + * @param src       the input data to be transformed.
> + * @param size      the size (in byte) of src.
> + * @param lowercase determines whether to use the range [0-9a-f] or [0-9A-F].
> + * @return buf.
> + */
>  char *ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase);
>  
>  /**
> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
> index 38ff90833a..0f912dd012 100644
> --- a/libavformat/movenc.c
> +++ b/libavformat/movenc.c
> @@ -4290,7 +4290,6 @@ static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *va
>      char buf[150];
>      len = FFMIN(sizeof(buf) / 2 - 1, len);
>      ff_data_to_hex(buf, value, len, 0);
> -    buf[2 * len] = '\0';
>      avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
>  }
>  
> diff --git a/libavformat/omadec.c b/libavformat/omadec.c
> index a727cc4d66..4c418a5ff2 100644
> --- a/libavformat/omadec.c
> +++ b/libavformat/omadec.c
> @@ -108,7 +108,6 @@ static void hex_log(AVFormatContext *s, int level,
>      if (av_log_get_level() < level)
>          return;
>      ff_data_to_hex(buf, value, len, 1);
> -    buf[len << 1] = '\0';
>      av_log(s, level, "%s: %s\n", name, buf);
>  }
>  
> diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c
> index b14d23b919..34020bc383 100644
> --- a/libavformat/rtmpproto.c
> +++ b/libavformat/rtmpproto.c
> @@ -1663,7 +1663,6 @@ static int do_llnw_auth(RTMPContext *rt, const char *user, const char *nonce)
>      av_md5_update(md5, rt->password, strlen(rt->password));
>      av_md5_final(md5, hash);
>      ff_data_to_hex(hashstr1, hash, 16, 1);
> -    hashstr1[32] = '\0';
>  
>      av_md5_init(md5);
>      av_md5_update(md5, method, strlen(method));
> @@ -1673,7 +1672,6 @@ static int do_llnw_auth(RTMPContext *rt, const char *user, const char *nonce)
>          av_md5_update(md5, "/_definst_", strlen("/_definst_"));
>      av_md5_final(md5, hash);
>      ff_data_to_hex(hashstr2, hash, 16, 1);
> -    hashstr2[32] = '\0';
>  
>      av_md5_init(md5);
>      av_md5_update(md5, hashstr1, strlen(hashstr1));
> diff --git a/libavformat/sdp.c b/libavformat/sdp.c
> index a41c2cf655..e83616cfbe 100644
> --- a/libavformat/sdp.c
> +++ b/libavformat/sdp.c
> @@ -216,7 +216,6 @@ static char *extradata2psets(AVFormatContext *s, AVCodecParameters *par)
>          memcpy(p, profile_string, strlen(profile_string));
>          p += strlen(p);
>          ff_data_to_hex(p, sps + 1, 3, 0);
> -        p[6] = '\0';
>      }
>      av_free(tmpbuf);
>  
> @@ -340,7 +339,6 @@ static char *extradata2config(AVFormatContext *s, AVCodecParameters *par)
>      }
>      memcpy(config, "; config=", 9);
>      ff_data_to_hex(config + 9, par->extradata, par->extradata_size, 0);
> -    config[9 + par->extradata_size * 2] = 0;
>  
>      return config;
>  }
> @@ -475,7 +473,6 @@ static char *latm_context2config(AVFormatContext *s, AVCodecParameters *par)
>          return NULL;
>      }
>      ff_data_to_hex(config, config_byte, 6, 1);
> -    config[12] = 0;
>  
>      return config;
>  }
> diff --git a/libavformat/takdec.c b/libavformat/takdec.c
> index 6bb26683b4..bb256e1190 100644
> --- a/libavformat/takdec.c
> +++ b/libavformat/takdec.c
> @@ -122,7 +122,6 @@ static int tak_read_header(AVFormatContext *s)
>              }
>  
>              ff_data_to_hex(md5_hex, md5, sizeof(md5), 1);
> -            md5_hex[2 * sizeof(md5)] = '\0';
>              av_log(s, AV_LOG_VERBOSE, "MD5=%s\n", md5_hex);
>              break;
>          }
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index 7840e8717c..c84f6a2348 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -1151,6 +1151,7 @@ char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
>          buff[i * 2]     = hex_table[src[i] >> 4];
>          buff[i * 2 + 1] = hex_table[src[i] & 0xF];
>      }
> +    buff[2 * s] = '\0';
>  
>      return buff;
>  }
> 

Will apply this patchset tomorrow unless there are objections.

- Andreas
diff mbox series

Patch

diff --git a/libavformat/hls.c b/libavformat/hls.c
index 52a031ed54..557faf8e8d 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -1284,7 +1284,6 @@  static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg,
         char iv[33], key[33], url[MAX_URL_SIZE];
         ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
         ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
-        iv[32] = key[32] = '\0';
         if (strstr(seg->url, "://"))
             snprintf(url, sizeof(url), "crypto+%s", seg->url);
         else
@@ -2074,7 +2073,6 @@  static int hls_read_header(AVFormatContext *s)
             if (strstr(in_fmt->name, "mov")) {
                 char key[33];
                 ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
-                key[32] = '\0';
                 av_dict_set(&options, "decryption_key", key, AV_OPT_FLAG_DECODING_PARAM);
             } else if (!c->crypto_ctx.aes_ctx) {
                 c->crypto_ctx.aes_ctx = av_aes_alloc();
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index cfd0c036d1..1c2a556375 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -745,7 +745,6 @@  static int do_encrypt(AVFormatContext *s, VariantStream *vs)
             memcpy(iv, hls->iv, sizeof(iv));
         }
         ff_data_to_hex(buf, iv, sizeof(iv), 0);
-        buf[32] = '\0';
         memcpy(hls->iv_string, buf, sizeof(hls->iv_string));
     }
 
diff --git a/libavformat/httpauth.c b/libavformat/httpauth.c
index 4f79c78edc..0a98ff80a5 100644
--- a/libavformat/httpauth.c
+++ b/libavformat/httpauth.c
@@ -156,7 +156,6 @@  static char *make_digest_auth(HTTPAuthState *state, const char *username,
     for (i = 0; i < 2; i++)
         cnonce_buf[i] = av_get_random_seed();
     ff_data_to_hex(cnonce, (const uint8_t*) cnonce_buf, sizeof(cnonce_buf), 1);
-    cnonce[2*sizeof(cnonce_buf)] = 0;
 
     md5ctx = av_md5_alloc();
     if (!md5ctx)
@@ -166,7 +165,6 @@  static char *make_digest_auth(HTTPAuthState *state, const char *username,
     update_md5_strings(md5ctx, username, ":", state->realm, ":", password, NULL);
     av_md5_final(md5ctx, hash);
     ff_data_to_hex(A1hash, hash, 16, 1);
-    A1hash[32] = 0;
 
     if (!strcmp(digest->algorithm, "") || !strcmp(digest->algorithm, "MD5")) {
     } else if (!strcmp(digest->algorithm, "MD5-sess")) {
@@ -174,7 +172,6 @@  static char *make_digest_auth(HTTPAuthState *state, const char *username,
         update_md5_strings(md5ctx, A1hash, ":", digest->nonce, ":", cnonce, NULL);
         av_md5_final(md5ctx, hash);
         ff_data_to_hex(A1hash, hash, 16, 1);
-        A1hash[32] = 0;
     } else {
         /* Unsupported algorithm */
         av_free(md5ctx);
@@ -185,7 +182,6 @@  static char *make_digest_auth(HTTPAuthState *state, const char *username,
     update_md5_strings(md5ctx, method, ":", uri, NULL);
     av_md5_final(md5ctx, hash);
     ff_data_to_hex(A2hash, hash, 16, 1);
-    A2hash[32] = 0;
 
     av_md5_init(md5ctx);
     update_md5_strings(md5ctx, A1hash, ":", digest->nonce, NULL);
@@ -195,7 +191,6 @@  static char *make_digest_auth(HTTPAuthState *state, const char *username,
     update_md5_strings(md5ctx, ":", A2hash, NULL);
     av_md5_final(md5ctx, hash);
     ff_data_to_hex(response, hash, 16, 1);
-    response[32] = 0;
 
     av_free(md5ctx);
 
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 20e93d9267..f43e408548 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -484,6 +484,17 @@  void ff_flush_packet_queue(AVFormatContext *s);
  */
 int ff_mkdir_p(const char *path);
 
+/**
+ * Write hexadecimal string corresponding to given binary data. The string
+ * is zero-terminated.
+ *
+ * @param buf       the output string is written here;
+ *                  needs to be at least 2 * size + 1 bytes long.
+ * @param src       the input data to be transformed.
+ * @param size      the size (in byte) of src.
+ * @param lowercase determines whether to use the range [0-9a-f] or [0-9A-F].
+ * @return buf.
+ */
 char *ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase);
 
 /**
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 38ff90833a..0f912dd012 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4290,7 +4290,6 @@  static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *va
     char buf[150];
     len = FFMIN(sizeof(buf) / 2 - 1, len);
     ff_data_to_hex(buf, value, len, 0);
-    buf[2 * len] = '\0';
     avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
 }
 
diff --git a/libavformat/omadec.c b/libavformat/omadec.c
index a727cc4d66..4c418a5ff2 100644
--- a/libavformat/omadec.c
+++ b/libavformat/omadec.c
@@ -108,7 +108,6 @@  static void hex_log(AVFormatContext *s, int level,
     if (av_log_get_level() < level)
         return;
     ff_data_to_hex(buf, value, len, 1);
-    buf[len << 1] = '\0';
     av_log(s, level, "%s: %s\n", name, buf);
 }
 
diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c
index b14d23b919..34020bc383 100644
--- a/libavformat/rtmpproto.c
+++ b/libavformat/rtmpproto.c
@@ -1663,7 +1663,6 @@  static int do_llnw_auth(RTMPContext *rt, const char *user, const char *nonce)
     av_md5_update(md5, rt->password, strlen(rt->password));
     av_md5_final(md5, hash);
     ff_data_to_hex(hashstr1, hash, 16, 1);
-    hashstr1[32] = '\0';
 
     av_md5_init(md5);
     av_md5_update(md5, method, strlen(method));
@@ -1673,7 +1672,6 @@  static int do_llnw_auth(RTMPContext *rt, const char *user, const char *nonce)
         av_md5_update(md5, "/_definst_", strlen("/_definst_"));
     av_md5_final(md5, hash);
     ff_data_to_hex(hashstr2, hash, 16, 1);
-    hashstr2[32] = '\0';
 
     av_md5_init(md5);
     av_md5_update(md5, hashstr1, strlen(hashstr1));
diff --git a/libavformat/sdp.c b/libavformat/sdp.c
index a41c2cf655..e83616cfbe 100644
--- a/libavformat/sdp.c
+++ b/libavformat/sdp.c
@@ -216,7 +216,6 @@  static char *extradata2psets(AVFormatContext *s, AVCodecParameters *par)
         memcpy(p, profile_string, strlen(profile_string));
         p += strlen(p);
         ff_data_to_hex(p, sps + 1, 3, 0);
-        p[6] = '\0';
     }
     av_free(tmpbuf);
 
@@ -340,7 +339,6 @@  static char *extradata2config(AVFormatContext *s, AVCodecParameters *par)
     }
     memcpy(config, "; config=", 9);
     ff_data_to_hex(config + 9, par->extradata, par->extradata_size, 0);
-    config[9 + par->extradata_size * 2] = 0;
 
     return config;
 }
@@ -475,7 +473,6 @@  static char *latm_context2config(AVFormatContext *s, AVCodecParameters *par)
         return NULL;
     }
     ff_data_to_hex(config, config_byte, 6, 1);
-    config[12] = 0;
 
     return config;
 }
diff --git a/libavformat/takdec.c b/libavformat/takdec.c
index 6bb26683b4..bb256e1190 100644
--- a/libavformat/takdec.c
+++ b/libavformat/takdec.c
@@ -122,7 +122,6 @@  static int tak_read_header(AVFormatContext *s)
             }
 
             ff_data_to_hex(md5_hex, md5, sizeof(md5), 1);
-            md5_hex[2 * sizeof(md5)] = '\0';
             av_log(s, AV_LOG_VERBOSE, "MD5=%s\n", md5_hex);
             break;
         }
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 7840e8717c..c84f6a2348 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -1151,6 +1151,7 @@  char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
         buff[i * 2]     = hex_table[src[i] >> 4];
         buff[i * 2 + 1] = hex_table[src[i] & 0xF];
     }
+    buff[2 * s] = '\0';
 
     return buff;
 }