From 657a953d5d2807ed99eddf7b45a4dc46cef626d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20=27Flameeyes=27=20Petten=C3=B2?= Date: Wed, 7 May 2008 18:23:55 +0200 Subject: Use strn?dup instead of alloc + strn?cpy. Whenever an allocated memory area is immediately filled in with a string through strcpy() or strncpy(), replace the calls with the appropriate strn?dup(). --- src/input/input_file.c | 15 +++++++-------- src/input/librtsp/rtsp.c | 4 +--- src/input/pnm.c | 8 +++++--- src/libsputext/demux_sputext.c | 26 ++++++++------------------ src/xine-engine/load_plugins.c | 3 +-- 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/input/input_file.c b/src/input/input_file.c index f07c9e93c..774f6b081 100644 --- a/src/input/input_file.c +++ b/src/input/input_file.c @@ -727,8 +727,9 @@ static xine_mrl_t **file_class_get_dir (input_class_t *this_gen, xprintf (this->xine, XINE_VERBOSITY_DEBUG, "input_file: readlink() failed: %s\n", strerror(errno)); else { - dir_files[num_dir_files].link = (char *) xine_xmalloc(linksize + 1); - strncpy(dir_files[num_dir_files].link, linkbuf, linksize); + dir_files[num_dir_files].link = + strndup(linkbuf, linksize); + dir_files[num_dir_files].type |= get_file_type(dir_files[num_dir_files].link, current_dir, this->xine); } } @@ -763,9 +764,8 @@ static xine_mrl_t **file_class_get_dir (input_class_t *this_gen, "input_file: readlink() failed: %s\n", strerror(errno)); } else { - hide_files[num_hide_files].link = (char *) - xine_xmalloc(linksize + 1); - strncpy(hide_files[num_hide_files].link, linkbuf, linksize); + hide_files[num_hide_files].link = + strndup(linkbuf, linksize); hide_files[num_hide_files].type |= get_file_type(hide_files[num_hide_files].link, current_dir, this->xine); } } @@ -796,9 +796,8 @@ static xine_mrl_t **file_class_get_dir (input_class_t *this_gen, "input_file: readlink() failed: %s\n", strerror(errno)); } else { - norm_files[num_norm_files].link = (char *) - xine_xmalloc(linksize + 1); - strncpy(norm_files[num_norm_files].link, linkbuf, linksize); + norm_files[num_norm_files].link = + strndup(linkbuf, linksize); norm_files[num_norm_files].type |= get_file_type(norm_files[num_norm_files].link, current_dir, this->xine); } } diff --git a/src/input/librtsp/rtsp.c b/src/input/librtsp/rtsp.c index beb77d8b4..99f0da665 100644 --- a/src/input/librtsp/rtsp.c +++ b/src/input/librtsp/rtsp.c @@ -482,9 +482,7 @@ rtsp_t *rtsp_connect(xine_stream_t *stream, const char *mrl, const char *user_ag pathbegin=slash-mrl_ptr; hostend=colon-mrl_ptr; - s->host = malloc(sizeof(char)*hostend+1); - strncpy(s->host, mrl_ptr, hostend); - s->host[hostend]=0; + s->host = strndup(mrl_ptr, hostend); if (pathbegin < strlen(mrl_ptr)) s->path=strdup(mrl_ptr+pathbegin+1); if (colon != slash) { diff --git a/src/input/pnm.c b/src/input/pnm.c index edd7a5d71..47a24a620 100644 --- a/src/input/pnm.c +++ b/src/input/pnm.c @@ -21,6 +21,10 @@ * based upon code from joschka */ +#ifdef HAVE_CONFIG_H +# include +#endif + #include #include #include @@ -732,9 +736,7 @@ pnm_t *pnm_connect(xine_stream_t *stream, const char *mrl) { pathbegin=slash-mrl_ptr; hostend=colon-mrl_ptr; - p->host = malloc(sizeof(char)*hostend+1); - strncpy(p->host, mrl_ptr, hostend); - p->host[hostend]=0; + p->host = strndup(mrl_ptr, hostend); if (pathbegin < strlen(mrl_ptr)) p->path=strdup(mrl_ptr+pathbegin+1); if (colon != slash) { diff --git a/src/libsputext/demux_sputext.c b/src/libsputext/demux_sputext.c index b159d3d7f..47b7a3361 100644 --- a/src/libsputext/demux_sputext.c +++ b/src/libsputext/demux_sputext.c @@ -262,12 +262,7 @@ static char *sub_readtext(char *source, char **dest) { p++,len++; } - *dest= (char *)xine_xmalloc (len+1); - if (!(*dest)) - return ERR; - - strncpy(*dest, source, len); - (*dest)[len]=0; + *dest = strndup(source, len); while (*p=='\r' || *p=='\n' || *p=='|') p++; @@ -333,10 +328,8 @@ static subtitle_t *sub_read_line_subviewer(demux_sputext_t *this, subtitle_t *cu p=q=line; for (current->lines=1; current->lines <= SUB_MAX_TEXT; current->lines++) { for (q=p,len=0; *p && *p!='\r' && *p!='\n' && *p!='|' && strncasecmp(p,"[br]",4); p++,len++); - current->text[current->lines-1]=(char *)xine_xmalloc (len+1); + current->text[current->lines-1] = strndup(q, len); if (!current->text[current->lines-1]) return ERR; - strncpy (current->text[current->lines-1], q, len); - current->text[current->lines-1][len]='\0'; if (!*p || *p=='\r' || *p=='\n') break; if (*p=='[') while (*p++!=']'); if (*p=='|') p++; @@ -396,10 +389,10 @@ static subtitle_t *sub_read_line_subrip(demux_sputext_t *this,subtitle_t *curren xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "Too many characters in a subtitle line\n"); if(temp_line[temp_index-1]=='\0' || temp_index==SUB_BUFSIZE) { if(temp_index>1) { /* more than 1 char (including '\0') -> that is a valid one */ - current->text[i]=(char *)xine_xmalloc(temp_index); + /* temp_index<=SUB_BUFSIZE is always true here */ + current->text[i] = strndup(temp_line, temp_index); if(!current->text[i]) return ERR; - strncpy(current->text[i],temp_line,temp_index); /* temp_index<=SUB_BUFSIZE is always true here */ i++; temp_index=0; } else @@ -565,9 +558,7 @@ static subtitle_t *sub_read_line_ssa(demux_sputext_t *this,subtitle_t *current) current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2; while (((tmp=strstr(line2, "\\n")) != NULL) || ((tmp=strstr(line2, "\\N")) != NULL) ){ - current->text[num]=(char *)malloc(tmp-line2+1); - strncpy (current->text[num], line2, tmp-line2); - current->text[num][tmp-line2]='\0'; + current->text[num] = strndup(line2, tmp-line2); line2=tmp+2; num++; current->lines++; @@ -930,10 +921,9 @@ static subtitle_t *sub_read_line_subviewer2(demux_sputext_t *this, subtitle_t *c len=0; for (p=line; *p!='\n' && *p!='\r' && *p; ++p,++len); if (len) { - current->text[i]=(char *)malloc (len+1); - if (!current->text[i]) return ERR; - strncpy (current->text[i], line, len); current->text[i][len]='\0'; - ++i; + current->text[i] = strndup(line, len); + if (!current->text[i]) return ERR; + ++i; } else { break; } diff --git a/src/xine-engine/load_plugins.c b/src/xine-engine/load_plugins.c index 9815b26c1..ed0106ffe 100644 --- a/src/xine-engine/load_plugins.c +++ b/src/xine-engine/load_plugins.c @@ -382,8 +382,7 @@ static void _insert_node (xine_t *this, * does not strdup() it, so we have to provide a different pointer * for each decoder */ for (i = 0; catalog->prio_desc[i]; i++); - catalog->prio_desc[i] = malloc(strlen(desc) + 1); - strcpy(catalog->prio_desc[i], desc); + catalog->prio_desc[i] = strdup(desc); this->config->register_num (this->config, key, 0, -- cgit v1.2.3