1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Get rid of asprintf and _GNU_SOURCE define

_GNU_SOURCE was even in some files where it was not needed at all
(http*).

Let's replace asprintf() with g_strdup_printf().
This commit is contained in:
Michael Vetter 2021-03-30 17:38:13 +02:00
parent 3c1e4bac3a
commit 1ec606540e
12 changed files with 97 additions and 104 deletions

View File

@ -36,8 +36,6 @@
#include "config.h" #include "config.h"
#define _GNU_SOURCE 1
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -1558,13 +1556,15 @@ cmd_ac_complete_filepath(const char* const input, char* const startstr, gboolean
// expand ~ to $HOME // expand ~ to $HOME
if (inpcp[0] == '~' && inpcp[1] == '/') { if (inpcp[0] == '~' && inpcp[1] == '/') {
if (asprintf(&tmp, "%s/%sfoo", getenv("HOME"), inpcp + 2) == -1) { tmp = g_strdup_printf("%s/%sfoo", getenv("HOME"), inpcp + 2);
if (!tmp) {
free(inpcp); free(inpcp);
return NULL; return NULL;
} }
output_off = strlen(getenv("HOME")) + 1; output_off = strlen(getenv("HOME")) + 1;
} else { } else {
if (asprintf(&tmp, "%sfoo", inpcp) == -1) { tmp = g_strdup_printf("%sfoo", inpcp);
if (!tmp) {
free(inpcp); free(inpcp);
return NULL; return NULL;
} }
@ -1596,25 +1596,29 @@ cmd_ac_complete_filepath(const char* const input, char* const startstr, gboolean
char* acstring; char* acstring;
if (output_off) { if (output_off) {
if (asprintf(&tmp, "%s/%s", directory, dir->d_name) == -1) { tmp = g_strdup_printf("%s/%s", directory, dir->d_name);
if(!tmp) {
free(directory); free(directory);
free(foofile); free(foofile);
return NULL; return NULL;
} }
if (asprintf(&acstring, "~/%s", tmp + output_off) == -1) { acstring = g_strdup_printf("~/%s", tmp + output_off);
if (!acstring) {
free(directory); free(directory);
free(foofile); free(foofile);
return NULL; return NULL;
} }
free(tmp); free(tmp);
} else if (strcmp(directory, "/") == 0) { } else if (strcmp(directory, "/") == 0) {
if (asprintf(&acstring, "/%s", dir->d_name) == -1) { acstring = g_strdup_printf("/%s", dir->d_name);
if (!acstring) {
free(directory); free(directory);
free(foofile); free(foofile);
return NULL; return NULL;
} }
} else { } else {
if (asprintf(&acstring, "%s/%s", directory, dir->d_name) == -1) { acstring = g_strdup_printf("%s/%s", directory, dir->d_name);
if (!acstring) {
free(directory); free(directory);
free(foofile); free(foofile);
return NULL; return NULL;

View File

@ -34,8 +34,6 @@
* *
*/ */
#define _GNU_SOURCE 1
#include "config.h" #include "config.h"
#include <assert.h> #include <assert.h>
@ -2894,11 +2892,11 @@ command_mangen(void)
mkdir_recursive("docs"); mkdir_recursive("docs");
char* header = NULL;
GDateTime* now = g_date_time_new_now_local(); GDateTime* now = g_date_time_new_now_local();
gchar* date = g_date_time_format(now, "%F"); gchar* date = g_date_time_format(now, "%F");
if (asprintf(&header, ".TH man 1 \"%s\" \"" PACKAGE_VERSION "\" \"Profanity XMPP client\"\n", date) == -1) { gchar *header = g_strdup_printf(".TH man 1 \"%s\" \"" PACKAGE_VERSION "\" \"Profanity XMPP client\"\n", date);
// TODO: error if (!header) {
log_error("command_mangen(): could not allocate memory");
return; return;
} }
g_date_time_unref(now); g_date_time_unref(now);
@ -2908,9 +2906,9 @@ command_mangen(void)
while (curr) { while (curr) {
Command* pcmd = curr->data; Command* pcmd = curr->data;
char* filename = NULL; gchar* filename = g_strdup_printf("docs/profanity-%s.1", &pcmd->cmd[1]);
if (asprintf(&filename, "docs/profanity-%s.1", &pcmd->cmd[1]) == -1) { if (!filename) {
// TODO: error log_error("command_mangen(): could not allocate memory");
return; return;
} }
FILE* manpage = fopen(filename, "w"); FILE* manpage = fopen(filename, "w");
@ -2955,6 +2953,6 @@ command_mangen(void)
printf("\nProcessed %d commands.\n\n", g_list_length(cmds)); printf("\nProcessed %d commands.\n\n", g_list_length(cmds));
free(header); g_free(header);
g_list_free(cmds); g_list_free(cmds);
} }

View File

@ -35,8 +35,6 @@
* *
*/ */
#define _GNU_SOURCE 1
#include "config.h" #include "config.h"
#include <string.h> #include <string.h>
@ -141,10 +139,10 @@ cmd_process_input(ProfWin* window, char* inp)
char* question_mark = strchr(command, '?'); char* question_mark = strchr(command, '?');
if (question_mark) { if (question_mark) {
*question_mark = '\0'; *question_mark = '\0';
char* fakeinp; gchar* fakeinp = g_strdup_printf("/help %s", command + 1);
if (asprintf(&fakeinp, "/help %s", command + 1)) { if (fakeinp) {
result = _cmd_execute(window, "/help", fakeinp); result = _cmd_execute(window, "/help", fakeinp);
free(fakeinp); g_free(fakeinp);
} }
} else { } else {
result = _cmd_execute(window, command, inp); result = _cmd_execute(window, command, inp);

View File

@ -34,8 +34,6 @@
* *
*/ */
#define _GNU_SOURCE 1
#include "config.h" #include "config.h"
#include <errno.h> #include <errno.h>
@ -529,13 +527,14 @@ _unique_filename(const char* filename)
unsigned int i = 0; unsigned int i = 0;
while (g_file_test(unique, G_FILE_TEST_EXISTS)) { while (g_file_test(unique, G_FILE_TEST_EXISTS)) {
free(unique); g_free(unique);
if (i > 1000) { // Give up after 1000 attempts. if (i > 1000) { // Give up after 1000 attempts.
return NULL; return NULL;
} }
if (asprintf(&unique, "%s.%u", filename, i) < 0) { unique = g_strdup_printf("%s.%u", filename, i);
if (!unique) {
return NULL; return NULL;
} }

View File

@ -35,8 +35,6 @@
#include "config.h" #include "config.h"
#define _GNU_SOURCE 1
#include <sys/stat.h> #include <sys/stat.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <glib.h> #include <glib.h>
@ -213,13 +211,14 @@ GSList*
log_database_get_previous_chat(const gchar* const contact_barejid) log_database_get_previous_chat(const gchar* const contact_barejid)
{ {
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
char* query; gchar* query;
const char* jid = connection_get_fulljid(); const char* jid = connection_get_fulljid();
Jid* myjid = jid_create(jid); Jid* myjid = jid_create(jid);
if (!myjid) if (!myjid)
return NULL; return NULL;
if (asprintf(&query, "SELECT * FROM (SELECT `message`, `timestamp`, `from_jid`, `type` from `ChatLogs` WHERE (`from_jid` = '%s' AND `to_jid` = '%s') OR (`from_jid` = '%s' AND `to_jid` = '%s') ORDER BY `timestamp` DESC LIMIT 10) ORDER BY `timestamp` ASC;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid) == -1) { query = g_strdup_printf("SELECT * FROM (SELECT `message`, `timestamp`, `from_jid`, `type` from `ChatLogs` WHERE (`from_jid` = '%s' AND `to_jid` = '%s') OR (`from_jid` = '%s' AND `to_jid` = '%s') ORDER BY `timestamp` DESC LIMIT 10) ORDER BY `timestamp` ASC;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid);
if (!query) {
log_error("log_database_get_previous_chat(): SQL query. could not allocate memory"); log_error("log_database_get_previous_chat(): SQL query. could not allocate memory");
return NULL; return NULL;
} }
@ -251,7 +250,7 @@ log_database_get_previous_chat(const gchar* const contact_barejid)
history = g_slist_append(history, msg); history = g_slist_append(history, msg);
} }
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
free(query); g_free(query);
return history; return history;
} }
@ -314,7 +313,7 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
} }
char* err_msg; char* err_msg;
char* query; gchar* query;
gchar* date_fmt; gchar* date_fmt;
if (message->timestamp) { if (message->timestamp) {
@ -331,7 +330,7 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
char* escaped_message = str_replace(message->plain, "'", "''"); char* escaped_message = str_replace(message->plain, "'", "''");
if (asprintf(&query, "INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `archive_id`, `replace_id`, `type`, `encryption`) SELECT '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' WHERE NOT EXISTS (SELECT 1 FROM `ChatLogs` WHERE `archive_id` = '%s')", query = g_strdup_printf("INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `archive_id`, `replace_id`, `type`, `encryption`) SELECT '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' WHERE NOT EXISTS (SELECT 1 FROM `ChatLogs` WHERE `archive_id` = '%s')",
from_jid->barejid, from_jid->barejid,
from_jid->resourcepart ? from_jid->resourcepart : "", from_jid->resourcepart ? from_jid->resourcepart : "",
to_jid->barejid, to_jid->barejid,
@ -343,8 +342,8 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
message->replace_id ? message->replace_id : "", message->replace_id ? message->replace_id : "",
type ? type : "", type ? type : "",
enc ? enc : "", enc ? enc : "",
message->stanzaid ? message->stanzaid : "") message->stanzaid ? message->stanzaid : "");
== -1) { if (!query) {
log_error("log_database_add(): SQL query. could not allocate memory"); log_error("log_database_add(): SQL query. could not allocate memory");
return; return;
} }
@ -359,5 +358,5 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
log_error("Unknown SQLite error"); log_error("Unknown SQLite error");
} }
} }
free(query); g_free(query);
} }

View File

@ -34,8 +34,6 @@
* *
*/ */
#define _GNU_SOURCE 1
#include "config.h" #include "config.h"
#include <stdlib.h> #include <stdlib.h>

View File

@ -35,7 +35,6 @@
#include "config.h" #include "config.h"
#define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -315,11 +314,13 @@ _autocomplete_param_common(const char* const input, char* command, autocomplete_
char* result = NULL; char* result = NULL;
int len; int len;
len = asprintf(&command_cpy, "%s ", command); command_cpy = g_strdup_printf("%s ", command);
if (len == -1) { if (!command_cpy) {
return NULL; return NULL;
} }
len = strlen(command_cpy);
if (strncmp(input, command_cpy, len) == 0) { if (strncmp(input, command_cpy, len) == 0) {
int inp_len = strlen(input); int inp_len = strlen(input);
char prefix[inp_len]; char prefix[inp_len];

View File

@ -35,8 +35,6 @@
#include "config.h" #include "config.h"
#define _GNU_SOURCE 1
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>

View File

@ -34,8 +34,6 @@
* *
*/ */
#define _GNU_SOURCE 1
#include "config.h" #include "config.h"
#include <stdlib.h> #include <stdlib.h>

View File

@ -33,8 +33,6 @@
* *
*/ */
#define _GNU_SOURCE 1
#include "config.h" #include "config.h"
#include <stdlib.h> #include <stdlib.h>
@ -92,12 +90,12 @@ _xferinfo(void* userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultot
ulperc = (100 * ulnow) / ultotal; ulperc = (100 * ulnow) / ultotal;
} }
char* msg; gchar* msg = g_strdup_printf("Uploading '%s': %d%%", upload->filename, ulperc);
if (asprintf(&msg, "Uploading '%s': %d%%", upload->filename, ulperc) == -1) { if (!msg) {
msg = strdup(FALLBACK_MSG); msg = g_strdup(FALLBACK_MSG);
} }
win_update_entry_message(upload->window, upload->put_url, msg); win_update_entry_message(upload->window, upload->put_url, msg);
free(msg); g_free(msg);
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
@ -165,11 +163,11 @@ http_file_put(void* userdata)
FILE* fh = NULL; FILE* fh = NULL;
char* err = NULL; char* err = NULL;
char* content_type_header; gchar* content_type_header;
// Optional headers // Optional headers
char* auth_header = NULL; gchar* auth_header = NULL;
char* cookie_header = NULL; gchar* cookie_header = NULL;
char* expires_header = NULL; gchar* expires_header = NULL;
CURL* curl; CURL* curl;
CURLcode res; CURLcode res;
@ -178,12 +176,12 @@ http_file_put(void* userdata)
upload->bytes_sent = 0; upload->bytes_sent = 0;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
char* msg; gchar* msg = g_strdup_printf("Uploading '%s': 0%%", upload->filename);
if (asprintf(&msg, "Uploading '%s': 0%%", upload->filename) == -1) { if (!msg) {
msg = strdup(FALLBACK_MSG); msg = g_strdup(FALLBACK_MSG);
} }
win_print_http_transfer(upload->window, msg, upload->put_url); win_print_http_transfer(upload->window, msg, upload->put_url);
free(msg); g_free(msg);
char* cert_path = prefs_get_string(PREF_TLS_CERTPATH); char* cert_path = prefs_get_string(PREF_TLS_CERTPATH);
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
@ -195,28 +193,32 @@ http_file_put(void* userdata)
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
struct curl_slist* headers = NULL; struct curl_slist* headers = NULL;
if (asprintf(&content_type_header, "Content-Type: %s", upload->mime_type) == -1) { content_type_header = g_strdup_printf("Content-Type: %s", upload->mime_type);
content_type_header = strdup(FALLBACK_CONTENTTYPE_HEADER); if (content_type_header) {
content_type_header = g_strdup(FALLBACK_CONTENTTYPE_HEADER);
} }
headers = curl_slist_append(headers, content_type_header); headers = curl_slist_append(headers, content_type_header);
headers = curl_slist_append(headers, "Expect:"); headers = curl_slist_append(headers, "Expect:");
// Optional headers // Optional headers
if (upload->authorization) { if (upload->authorization) {
if (asprintf(&auth_header, "Authorization: %s", upload->authorization) == -1) { auth_header = g_strdup_printf("Authorization: %s", upload->authorization);
auth_header = strdup(FALLBACK_MSG); if (!auth_header) {
auth_header = g_strdup(FALLBACK_MSG);
} }
headers = curl_slist_append(headers, auth_header); headers = curl_slist_append(headers, auth_header);
} }
if (upload->cookie) { if (upload->cookie) {
if (asprintf(&cookie_header, "Cookie: %s", upload->cookie) == -1) { cookie_header = g_strdup_printf("Cookie: %s", upload->cookie);
cookie_header = strdup(FALLBACK_MSG); if (!cookie_header) {
cookie_header = g_strdup(FALLBACK_MSG);
} }
headers = curl_slist_append(headers, cookie_header); headers = curl_slist_append(headers, cookie_header);
} }
if (upload->expires) { if (upload->expires) {
if (asprintf(&expires_header, "Expires: %s", upload->expires) == -1) { expires_header = g_strdup_printf("Expires: %s", upload->expires);
expires_header = strdup(FALLBACK_MSG); if (!expires_header) {
expires_header = g_strdup(FALLBACK_MSG);
} }
headers = curl_slist_append(headers, expires_header); headers = curl_slist_append(headers, expires_header);
} }
@ -262,9 +264,7 @@ http_file_put(void* userdata)
// XEP-0363 specifies 201 but prosody returns 200 // XEP-0363 specifies 201 but prosody returns 200
if (http_code != 200 && http_code != 201) { if (http_code != 200 && http_code != 201) {
if (asprintf(&err, "Server returned %lu", http_code) == -1) { err = g_strdup_printf("Server returned %lu", http_code);
err = NULL;
}
} }
#if 0 #if 0
@ -281,47 +281,50 @@ http_file_put(void* userdata)
if (fh) { if (fh) {
fclose(fh); fclose(fh);
} }
free(content_type_header);
free(output.buffer); free(output.buffer);
free(auth_header); g_free(content_type_header);
free(cookie_header); g_free(auth_header);
free(expires_header); g_free(cookie_header);
g_free(expires_header);
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
g_free(cert_path); g_free(cert_path);
if (err) { if (err) {
char* msg; gchar* msg;
if (upload->cancel) { if (upload->cancel) {
if (asprintf(&msg, "Uploading '%s' failed: Upload was canceled", upload->filename) == -1) { msg = g_strdup_printf("Uploading '%s' failed: Upload was canceled", upload->filename);
msg = strdup(FALLBACK_MSG); if (!msg) {
msg = g_strdup(FALLBACK_MSG);
} }
} else { } else {
if (asprintf(&msg, "Uploading '%s' failed: %s", upload->filename, err) == -1) { msg = g_strdup_printf("Uploading '%s' failed: %s", upload->filename, err);
msg = strdup(FALLBACK_MSG); if (!msg) {
msg = g_strdup(FALLBACK_MSG);
} }
win_update_entry_message(upload->window, upload->put_url, msg); win_update_entry_message(upload->window, upload->put_url, msg);
} }
cons_show_error(msg); cons_show_error(msg);
free(msg); g_free(msg);
free(err); free(err);
} else { } else {
if (!upload->cancel) { if (!upload->cancel) {
if (asprintf(&msg, "Uploading '%s': 100%%", upload->filename) == -1) { msg = g_strdup_printf("Uploading '%s': 100%%", upload->filename);
msg = strdup(FALLBACK_MSG); if (!msg) {
msg = g_strdup(FALLBACK_MSG);
} }
win_update_entry_message(upload->window, upload->put_url, msg); win_update_entry_message(upload->window, upload->put_url, msg);
win_mark_received(upload->window, upload->put_url); win_mark_received(upload->window, upload->put_url);
free(msg); g_free(msg);
char* url = NULL; char* url = NULL;
if (format_alt_url(upload->get_url, upload->alt_scheme, upload->alt_fragment, &url) != 0) { if (format_alt_url(upload->get_url, upload->alt_scheme, upload->alt_fragment, &url) != 0) {
char* msg; gchar* msg = g_strdup_printf("Uploading '%s' failed: Bad URL ('%s')", upload->filename, upload->get_url);
if (asprintf(&msg, "Uploading '%s' failed: Bad URL ('%s')", upload->filename, upload->get_url) == -1) { if (!msg) {
msg = strdup(FALLBACK_MSG); msg = g_strdup(FALLBACK_MSG);
} }
cons_show_error(msg); cons_show_error(msg);
free(msg); g_free(msg);
} else { } else {
switch (upload->window->type) { switch (upload->window->type) {
case WIN_CHAT: case WIN_CHAT:

View File

@ -37,7 +37,6 @@
#include "config.h" #include "config.h"
#include "ui.h" #include "ui.h"
#define _GNU_SOURCE 1
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>

View File

@ -33,8 +33,6 @@
* *
*/ */
#define _GNU_SOURCE 1
#include "config.h" #include "config.h"
#ifdef HAVE_GIT_VERSION #ifdef HAVE_GIT_VERSION
@ -242,10 +240,10 @@ stanza_create_http_upload_request(xmpp_ctx_t* ctx, const char* const id,
xmpp_stanza_set_attribute(request, STANZA_ATTR_FILENAME, basename(filename_cpy)); xmpp_stanza_set_attribute(request, STANZA_ATTR_FILENAME, basename(filename_cpy));
free(filename_cpy); free(filename_cpy);
char* filesize = NULL; gchar* filesize = g_strdup_printf("%jd", (intmax_t)(upload->filesize));
if (asprintf(&filesize, "%jd", (intmax_t)(upload->filesize)) != -1) { if (filesize) {
xmpp_stanza_set_attribute(request, STANZA_ATTR_SIZE, filesize); xmpp_stanza_set_attribute(request, STANZA_ATTR_SIZE, filesize);
free(filesize); g_free(filesize);
} }
xmpp_stanza_set_attribute(request, STANZA_ATTR_CONTENTTYPE, upload->mime_type); xmpp_stanza_set_attribute(request, STANZA_ATTR_CONTENTTYPE, upload->mime_type);