Author: igalic
Date: Sun Sep 18 18:30:38 2011
New Revision: 1172315
URL: http://svn.apache.org/viewvc?rev=1172315&view=rev
Log:
TS-953: consolidate string copy/concat for proxy/http/ - passing regressions,
thanks amc!
Modified:
trafficserver/traffic/trunk/proxy/http/HttpBodyFactory.cc
trafficserver/traffic/trunk/proxy/http/HttpConfig.cc
trafficserver/traffic/trunk/proxy/http/HttpMessageBody.cc
trafficserver/traffic/trunk/proxy/http/HttpSM.cc
trafficserver/traffic/trunk/proxy/http/HttpTransact.cc
trafficserver/traffic/trunk/proxy/http/HttpTransactCache.cc
Modified: trafficserver/traffic/trunk/proxy/http/HttpBodyFactory.cc
URL:
http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/http/HttpBodyFactory.cc?rev=1172315&r1=1172314&r2=1172315&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/http/HttpBodyFactory.cc (original)
+++ trafficserver/traffic/trunk/proxy/http/HttpBodyFactory.cc Sun Sep 18
18:30:38 2011
@@ -78,8 +78,8 @@ HttpBodyFactory::fabricate_with_old_api(
*resulting_buffer_length = 0;
- ink_strncpy(content_language_out_buf, "en", content_language_buf_size);
- ink_strncpy(content_type_out_buf, "text/html", content_type_buf_size);
+ ink_strlcpy(content_language_out_buf, "en", content_language_buf_size);
+ ink_strlcpy(content_type_out_buf, "text/html", content_type_buf_size);
///////////////////////////////////////////////////////////////////
// if logging turned on, buffer up the URL string for simplicity //
@@ -1007,9 +1007,7 @@ HttpBodyTemplate::load_from_file(char *d
reset();
template_buffer = new_template_buffer;
byte_count = new_byte_count;
- size_t pathlen = strlen(path) + 1;
- template_pathname = (char *)ats_malloc(pathlen);
- ink_strncpy(template_pathname, path, pathlen);
+ template_pathname = ats_strdup(path);
return (1);
}
Modified: trafficserver/traffic/trunk/proxy/http/HttpConfig.cc
URL:
http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/http/HttpConfig.cc?rev=1172315&r1=1172314&r2=1172315&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/http/HttpConfig.cc (original)
+++ trafficserver/traffic/trunk/proxy/http/HttpConfig.cc Sun Sep 18 18:30:38
2011
@@ -1681,17 +1681,14 @@ read_string(FILE * fp, char *buf, int si
}
static bool
-store_error_message(char *err_msg_buf, int buf_size, const char *fmt, ...)
+store_error_message(char *err_msg_buf, int err_msg_buf_size, const char *fmt,
...)
{
- if (likely(err_msg_buf && buf_size > 0)) {
+ if (likely(err_msg_buf && err_msg_buf_size > 0)) {
char buf[2048];
va_list ap;
va_start(ap, fmt);
(void) vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
- buf[sizeof(buf) - 1] = 0;
- err_msg_buf[0] = 0;
- strncpy(err_msg_buf, buf, buf_size - 1);
- err_msg_buf[buf_size - 1] = 0;
+ ink_strlcpy(err_msg_buf, buf, err_msg_buf_size);
va_end(ap);
}
return false;
@@ -1821,9 +1818,8 @@ HttpUserAgent_RegxEntry::create(char *_r
if (_refexp_str && *_refexp_str) {
- strncpy(refexp_str_buf, _refexp_str, sizeof(refexp_str_buf) - 1);
- refexp_str_buf[sizeof(refexp_str_buf) - 1] = 0;
- refexp_str = &refexp_str_buf[0];
+ ink_strlcpy(refexp_str_buf, _refexp_str, sizeof(refexp_str_buf));
+ refexp_str = refexp_str_buf;
Debug("http_aeua", "[HttpUserAgent_RegxEntry::create] - \"%s\"",
refexp_str);
while (*refexp_str && (*refexp_str == ' ' || *refexp_str == '\t'))
@@ -1855,7 +1851,7 @@ HttpUserAgent_RegxEntry::create(char *_r
regx = pcre_compile((const char *) user_agent_str, PCRE_CASELESS,
&error, &erroffset, NULL);
if (regx == NULL) {
if (errmsgbuf && (errmsgbuf_size - 1) > 0)
- ink_strncpy(errmsgbuf, error, errmsgbuf_size - 1);
+ ink_strlcpy(errmsgbuf, error, errmsgbuf_size);
user_agent_str = (char *)ats_free_null(user_agent_str);
retcode = false;
} else
Modified: trafficserver/traffic/trunk/proxy/http/HttpMessageBody.cc
URL:
http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/http/HttpMessageBody.cc?rev=1172315&r1=1172314&r2=1172315&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/http/HttpMessageBody.cc (original)
+++ trafficserver/traffic/trunk/proxy/http/HttpMessageBody.cc Sun Sep 18
18:30:38 2011
@@ -77,8 +77,7 @@ HttpMessageBody::MakeErrorBodyVA(int64_t
l = 0;
p = outbuf;
- error_title[sizeof(error_title) - 1] = '\0';
- strncpy(error_title, reason, sizeof(error_title) - 1);
+ ink_strlcpy(error_title, reason, sizeof(error_title));
p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
l += ink_bsprintf(p, "<HEAD><TITLE>%s</TITLE></HEAD>\n", error_title) - 1;
Modified: trafficserver/traffic/trunk/proxy/http/HttpSM.cc
URL:
http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/http/HttpSM.cc?rev=1172315&r1=1172314&r2=1172315&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/http/HttpSM.cc (original)
+++ trafficserver/traffic/trunk/proxy/http/HttpSM.cc Sun Sep 18 18:30:38 2011
@@ -1948,8 +1948,7 @@ HttpSM::process_srv_info(HostDBInfo * r)
/* we didnt get any SRV records, continue w normal lookup */
if (!r->srv_count) {
Debug("dns_srv", "No SRV records were available, continuing to lookup %s",
t_state.dns_info.lookup_name);
- strncpy(&new_host[0], t_state.dns_info.lookup_name, MAXDNAME);
- new_host[MAXDNAME - 1] = '\0';
+ ink_strlcpy(new_host, t_state.dns_info.lookup_name, sizeof(new_host));
goto lookup;
}
@@ -1957,8 +1956,7 @@ HttpSM::process_srv_info(HostDBInfo * r)
if (*new_host == '\0') {
Debug("dns_srv", "Weighted host returned was NULL or blank!, using %s as
origin", t_state.dns_info.lookup_name);
- strncpy(&new_host[0], t_state.dns_info.lookup_name, MAXDNAME);
- new_host[MAXDNAME - 1] = '\0';
+ ink_strlcpy(new_host, t_state.dns_info.lookup_name, sizeof(new_host));
} else {
Debug("dns_srv", "Weighted host now: %s", new_host);
}
@@ -3715,9 +3713,8 @@ HttpSM::do_hostdb_lookup()
if (use_srv_records) {
char* d = t_state.dns_info.srv_hostname;
- memcpy(d, "_http._tcp.", 11);
- ink_strncpy(d+11, t_state.server_info.name, MAXDNAME - 11);
- d[MAXDNAME - 1] = '\0'; // Just to be sure it's NULL terminated in case of
truncation
+ memcpy(d, "_http._tcp.", 11); // don't copy '\0'
+ ink_strlcpy(d + 11, t_state.server_info.name, sizeof(d) - 11 ); // all in
the name of performance!
Debug("dns_srv", "Beginning lookup of SRV records for origin %s", d);
HTTP_SM_SET_DEFAULT_HANDLER(&HttpSM::state_srv_lookup);
@@ -6154,28 +6151,19 @@ HttpSM::update_stats()
ink_hrtime_from_msec(t_state.http_config_param->slow_log_threshold) <
total_time) {
// get the url to log
URL *url = t_state.hdr_info.client_request.url_get();
- char url_string[256];
+ char url_string[256] = "";
if (url != NULL && url->valid()) {
- url->string_get_buf(url_string, 256);
- } else {
- strncpy(url_string, "", sizeof(url_string));
+ url->string_get_buf(url_string, sizeof(url_string));
}
// unique id
- char unique_id_string[128];
+ char unique_id_string[128] = "";
if (url != NULL && url->valid()) {
int length = 0;
const char *field =
t_state.hdr_info.client_request.value_get(MIME_FIELD_X_ID, MIME_LEN_X_ID,
&length);
if (field != NULL) {
- if ((int)sizeof(unique_id_string) < length)
- length = (int)sizeof(unique_id_string);
- strncpy(unique_id_string, field, length);
- unique_id_string[length] = '\0';
- } else {
- strncpy(unique_id_string, "", sizeof(unique_id_string));
+ ink_strlcpy(unique_id_string, field, sizeof(unique_id_string));
}
- } else {
- strncpy(unique_id_string, "", sizeof(unique_id_string));
}
// set the fd for the request
@@ -6932,7 +6920,7 @@ HttpSM::redirect_request(const char *red
#else
char *buf = (char *)ats_malloc(host_len + 7);
#endif
- strncpy(buf, host, host_len);
+ ink_strlcpy(buf, host, host_len);
host_len += snprintf(buf + host_len, sizeof(buf) - host_len, ":%d",
port);
t_state.hdr_info.client_request.value_set(MIME_FIELD_HOST,
MIME_LEN_HOST, buf, host_len);
#if !defined(__GNUC__)
Modified: trafficserver/traffic/trunk/proxy/http/HttpTransact.cc
URL:
http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/http/HttpTransact.cc?rev=1172315&r1=1172314&r2=1172315&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/http/HttpTransact.cc (original)
+++ trafficserver/traffic/trunk/proxy/http/HttpTransact.cc Sun Sep 18 18:30:38
2011
@@ -524,11 +524,10 @@ how_to_open_connection(HttpTransact::Sta
// to the default port.
int port = url->port_get();
if (port != url_canonicalize_port(URL_TYPE_HTTP, 0)) {
- char *buf = (char *)ats_malloc(host_len + 15);
- strncpy(buf, host, host_len);
- host_len += snprintf(buf + host_len, host_len + 15, ":%d", port);
+ char *buf = (char *)alloca(host_len + 15);
+ memcpy(buf, host, host_len);
+ host_len += snprintf(buf + host_len, 15, ":%d", port);
s->hdr_info.server_request.value_set(MIME_FIELD_HOST, MIME_LEN_HOST,
buf, host_len);
- ats_free(buf);
} else {
s->hdr_info.server_request.value_set(MIME_FIELD_HOST, MIME_LEN_HOST,
host, host_len);
}
@@ -1039,9 +1038,9 @@ HttpTransact::ModifyRequest(State* s)
char *buf = NULL;
if (port > 0) {
- buf = (char *)ats_malloc(host_val_len + 15);
- strncpy(buf, hostname, host_val_len);
- host_val_len += snprintf(buf + host_val_len, host_val_len + 15, ":%d",
port);
+ buf = (char *)alloca(host_val_len + 15);
+ memcpy(buf, hostname, host_val_len);
+ host_val_len += snprintf(buf + host_val_len, 15, ":%d", port);
host_val = (const char**)(&buf);
}
@@ -1059,7 +1058,6 @@ HttpTransact::ModifyRequest(State* s)
s->hdr_info.client_request.field_value_set(host_field, *host_val,
host_val_len);
}
- ats_free(buf);
request->mark_target_dirty();
}
@@ -3643,8 +3641,7 @@ HttpTransact::delete_srv_entry(State* s,
rr_data->info[i].srv_priority = srv_entry->getPriority();
rr_data->info[i].srv_port = srv_entry->getPort();
- ink_strncpy(rr_data->rr_srv_hosts[i], srv_entry->getHost(),
MAXDNAME);
- rr_data->rr_srv_hosts[i][MAXDNAME - 1] = '\0';
+ ink_strlcpy(rr_data->rr_srv_hosts[i], srv_entry->getHost(),
sizeof(rr_data->rr_srv_hosts[i]));
rr_data->info[i].is_srv = true;
rr_data->info[i].md5_high = new_r->md5_high;
@@ -7805,11 +7802,10 @@ HttpTransact::build_request(State* s, HT
// to the default port.
int port = url->port_get();
if (port != url_canonicalize_port(URL_TYPE_HTTP, 0)) {
- char *buf = (char *) ats_malloc(host_len + 15);
- strncpy(buf, host, host_len);
- host_len += snprintf(buf + host_len, host_len + 15, ":%d", port);
+ char *buf = (char *) alloca(host_len + 15);
+ memcpy(buf, host, host_len);
+ host_len += snprintf(buf + host_len, 15, ":%d", port);
outgoing_request->value_set(MIME_FIELD_HOST, MIME_LEN_HOST, buf,
host_len);
- ats_free(buf);
} else {
outgoing_request->value_set(MIME_FIELD_HOST, MIME_LEN_HOST, host,
host_len);
}
Modified: trafficserver/traffic/trunk/proxy/http/HttpTransactCache.cc
URL:
http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/http/HttpTransactCache.cc?rev=1172315&r1=1172314&r2=1172315&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/http/HttpTransactCache.cc (original)
+++ trafficserver/traffic/trunk/proxy/http/HttpTransactCache.cc Sun Sep 18
18:30:38 2011
@@ -646,7 +646,7 @@ HttpTransactCache::calculate_quality_of_
// get the charset of this content-type //
c_raw = content_field->value_get(&c_raw_len);
if (!HttpCompat::lookup_param_in_semicolon_string(c_raw, c_raw_len,
"charset", c_charset, sizeof(c_charset) - 1)) {
- ink_strncpy(c_charset, default_charset, sizeof(c_charset));
+ ink_strlcpy(c_charset, default_charset, sizeof(c_charset));
}
// Now loop over Accept-Charset field values.
// TODO: Should we check the return value (count) from this?
@@ -1516,19 +1516,19 @@ CacheLookupHttpConfig::marshal(char *buf
len = (cache_vary_default_text ? strlen(cache_vary_default_text) + 1 : 1);
if ((length -= len) < 0)
return -1;
- ink_strncpy(p, (cache_vary_default_text ? cache_vary_default_text : ""),
len);
+ ink_strlcpy(p, (cache_vary_default_text ? cache_vary_default_text : ""),
length);
p += len;
len = (cache_vary_default_images ? strlen(cache_vary_default_images) + 1 :
1);
if ((length -= len) < 0)
return -1;
- ink_strncpy(p, (cache_vary_default_images ? cache_vary_default_images : ""),
len);
+ ink_strlcpy(p, (cache_vary_default_images ? cache_vary_default_images : ""),
length);
p += len;
len = (cache_vary_default_other ? strlen(cache_vary_default_other) + 1 : 1);
if ((length -= len) < 0)
return -1;
- ink_strncpy(p, (cache_vary_default_other ? cache_vary_default_other : ""),
len);
+ ink_strlcpy(p, (cache_vary_default_other ? cache_vary_default_other : ""),
length);
p += len;
return (p - buf);