-
Notifications
You must be signed in to change notification settings - Fork 54
Improve use of sprintf and snprintf #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
b628b89
96d3b1f
90e6b02
42666a2
599c38f
c320b45
f192e90
e199667
8edf26e
2b7442b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,8 +67,8 @@ typedef struct { | |
|
|
||
| // Globally accessable config options | ||
| typedef struct { | ||
| char bitcoind_rpcuser[256]; | ||
| char bitcoind_rpcpassword[256]; | ||
| char bitcoind_rpcuser[128]; | ||
| char bitcoind_rpcpassword[128]; | ||
|
Comment on lines
+71
to
+72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just replace these with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, removed the global. In order to easily parse user and pass from the config I made rpcuser and rpcpassword char arrays in datum_conf.c. Is what's in the last commit ok? c320b45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That moves the individual user/pass to globals :( Maybe check out what I did in #60
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I initially wanted to just put rpcuserpass in global_config_t, and I really wanted to some how get rid of bitcoind_rpcuser and bitcoind_rpcpassword from it as you said (replace). That would require copying the user and password parts into the same array and "rejigging" it to put the colon in (%s:%s) That would allow to get rid of rpcuser and rpcpassword, but I didn't like it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| char bitcoind_rpcurl[256]; | ||
| int bitcoind_work_update_seconds; | ||
| bool bitcoind_notify_fallback; | ||
|
|
@@ -126,6 +126,7 @@ typedef struct { | |
| } global_config_t; | ||
|
|
||
| extern global_config_t datum_config; | ||
| extern char userpass[sizeof(datum_config.bitcoind_rpcuser) + sizeof(datum_config.bitcoind_rpcpassword)]; | ||
|
|
||
| int datum_read_config(const char *conffile); | ||
| void datum_gateway_help(void); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,7 +118,7 @@ int datum_logger_queue_msg(const char *func, int level, const char *format, ...) | |
| va_list args; | ||
| struct timeval tv; | ||
| struct tm tm_info; | ||
| char time_buffer[40]; | ||
| char time_buffer[20]; | ||
|
|
||
| if ((level < log_level_console) && (level < log_level_file)) { | ||
| return 0; | ||
|
|
@@ -202,7 +202,12 @@ int datum_logger_queue_msg(const char *func, int level, const char *format, ...) | |
| msg->msg = &msg_buffer[buffer_id][msg_buf_idx[buffer_id]]; | ||
| va_start(args, format); | ||
| i = vsnprintf(msg->msg, 1023, format, args); | ||
| msg->msg[i] = 0; | ||
|
|
||
| // clamp i to actual written value in order not to waste buffer space | ||
| if (i >= 1023) { | ||
| i = 1022; | ||
| } | ||
|
|
||
| va_end(args); | ||
|
|
||
| if (((msg_buf_idx[buffer_id]+i+2) > msg_buf_maxsz) || (dlog_queue_next[buffer_id] >= dlog_queue_max_entries)) { | ||
|
|
@@ -251,7 +256,7 @@ void * datum_logger_thread(void *ptr) { | |
| struct tm tm_info_storage; | ||
| struct tm *tm_info; | ||
| DLOG_MSG *msg; | ||
| char time_buffer[40]; | ||
| char time_buffer[20]; | ||
| char log_line[1200]; | ||
| FILE *log_handle = NULL; | ||
| time_t next_log_rotate = get_midnight_timestamp(); | ||
|
|
@@ -348,9 +353,9 @@ void * datum_logger_thread(void *ptr) { | |
| tm_info = localtime_r(&seconds, &tm_info_storage); | ||
| strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", tm_info); | ||
| if (log_calling_function) { | ||
| j = snprintf(log_line, 1199, "%s.%03d [%44s] %s: %s\n", time_buffer, millis, msg->calling_function, level_text[msg->level], msg->msg); | ||
| j = snprintf(log_line, sizeof(log_line), "%s.%03d [%44s] %s: %s\n", time_buffer, millis, msg->calling_function, level_text[msg->level], msg->msg); | ||
| } else { | ||
| j = snprintf(log_line, 1199, "%s.%03d %s: %s\n", time_buffer, millis, level_text[msg->level], msg->msg); | ||
| j = snprintf(log_line, sizeof(log_line), "%s.%03d %s: %s\n", time_buffer, millis, level_text[msg->level], msg->msg); | ||
| } | ||
| log_line[1199] = 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should drop this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we set NUL at 1199 anyway? We can. I just wanted to reduce relying on hardcoded lengths and make the code use defined char arrays sizes and recommended ways to call the "snprintf family". What do you think? |
||
|
|
||
|
|
@@ -390,8 +395,7 @@ void * datum_logger_thread(void *ptr) { | |
|
|
||
| tm_info = localtime_r(&log_file_opened, &tm_info_storage); | ||
| strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d", tm_info); | ||
| snprintf(log_line, 1199, "%s.%s", log_file, time_buffer); | ||
| log_line[1199] = 0; | ||
| snprintf(log_line, sizeof(log_line), "%s.%s", log_file, time_buffer); | ||
|
|
||
| fclose(log_handle); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.