PostgreSQL Source Code  git master
psprintf.c File Reference
#include "postgres.h"
#include "utils/memutils.h"
Include dependency graph for psprintf.c:

Go to the source code of this file.

Functions

char * psprintf (const char *fmt,...)
 
size_t pvsnprintf (char *buf, size_t len, const char *fmt, va_list args)
 

Function Documentation

◆ psprintf()

char* psprintf ( const char *  fmt,
  ... 
)

Definition at line 46 of file psprintf.c.

References generate_unaccent_rules::args, palloc(), pfree(), and pvsnprintf().

Referenced by _mdfd_segpath(), anytime_typmodout(), anytimestamp_typmodout(), auth_failed(), AuxiliaryProcessMain(), BaseBackup(), brin_desummarize_range(), brin_metapage_info(), brin_page_type(), brin_summarize_range(), bt_metap(), bt_page_print_tuples(), bt_page_stats(), bt_target_page_check(), build_function_result_tupdesc_d(), build_server_final_message(), build_server_first_message(), cash_out(), cfopen_read(), cfopen_write(), CheckSCRAMAuth(), complex_out(), create_script_for_cluster_analyze(), create_script_for_old_cluster_deletion(), create_tablespace_directories(), create_xlog_or_symlink(), datasegpath(), dblink_get_pkey(), defGetString(), describeOneTableDetails(), destroy_tablespace_directories(), do_shell(), do_to_timestamp(), doputenv(), dumpAttrDef(), dumpConstraint(), dumpPolicy(), dumpPublicationTable(), dumpRule(), dumpTrigger(), ecpg_start_test(), editFile(), exec_command_setenv(), expand_dynamic_library_name(), expand_tilde(), ExplainNode(), ExplainPropertyFloat(), fetch_finfo_record(), float4_to_char(), float8_to_char(), format_type_extended(), get_role_password(), get_user_name(), GetDatabasePath(), getFormattedOperatorName(), gethba_options(), getObjectIdentityParts(), GetRelationPath(), GUCArrayAdd(), HandleParallelMessage(), helpSQL(), ident_inet(), initialize_data_directory(), initialize_environment(), initializeInput(), int4_to_char(), isolation_start_test(), line_out(), llvm_compile_module(), llvm_expand_funcname(), llvm_function_reference(), load_libraries(), log_line_prefix(), logicalrep_typmap_gettypname(), LogicalRepSyncTableStart(), main(), md5_crypt_verify(), parse_hba_auth_opt(), parse_hba_line(), parseCommandLine(), perform_base_backup(), pg_be_scram_init(), pg_control_checkpoint(), pg_get_multixact_members(), pg_import_system_collations(), pg_logdir_ls_internal(), pg_putenv(), pg_size_pretty_numeric(), pg_tablespace_databases(), pgstatindex_impl(), plain_crypt_verify(), printTypmod(), process_psqlrc_file(), ProcessStartupPacket(), prompt_for_password(), pset_value_string(), psql_start_test(), readCommandResponse(), sequence_options(), set_input(), set_null_conf(), set_tablespace_directory_suffix(), setup_pgdata(), spawn_process(), sql_exec_searchtables(), SS_make_initplan_from_plan(), SS_process_ctes(), StartLogStreamer(), StartupXLOG(), StoreQueryTuple(), substitute_libpath_macro(), tokenize_file(), tokenize_inc_file(), TransactionIdInRecentPast(), try_complete_step(), typenameTypeMod(), widget_out(), write_version_file(), XLogDumpDisplayStats(), and xstrcat().

47 {
48  int save_errno = errno;
49  size_t len = 128; /* initial assumption about buffer size */
50 
51  for (;;)
52  {
53  char *result;
54  va_list args;
55  size_t newlen;
56 
57  /*
58  * Allocate result buffer. Note that in frontend this maps to malloc
59  * with exit-on-error.
60  */
61  result = (char *) palloc(len);
62 
63  /* Try to format the data. */
64  errno = save_errno;
65  va_start(args, fmt);
66  newlen = pvsnprintf(result, len, fmt, args);
67  va_end(args);
68 
69  if (newlen < len)
70  return result; /* success */
71 
72  /* Release buffer and loop around to try again with larger len. */
73  pfree(result);
74  len = newlen;
75  }
76 }
void pfree(void *pointer)
Definition: mcxt.c:1056
size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
Definition: psprintf.c:106
void * palloc(Size size)
Definition: mcxt.c:949

◆ pvsnprintf()

size_t pvsnprintf ( char *  buf,
size_t  len,
const char *  fmt,
va_list  args 
)

Definition at line 106 of file psprintf.c.

References _, elog, ereport, errcode(), errmsg(), ERROR, EXIT_FAILURE, fprintf, MaxAllocSize, strerror, unlikely, and vsnprintf.

Referenced by ahprintf(), appendStringInfoVA(), archprintf(), psprintf(), and tarPrintf().

107 {
108  int nprinted;
109 
110  nprinted = vsnprintf(buf, len, fmt, args);
111 
112  /* We assume failure means the fmt is bogus, hence hard failure is OK */
113  if (unlikely(nprinted < 0))
114  {
115 #ifndef FRONTEND
116  elog(ERROR, "vsnprintf failed: %m with format string \"%s\"", fmt);
117 #else
118  fprintf(stderr, "vsnprintf failed: %s with format string \"%s\"\n",
119  strerror(errno), fmt);
120  exit(EXIT_FAILURE);
121 #endif
122  }
123 
124  if ((size_t) nprinted < len)
125  {
126  /* Success. Note nprinted does not include trailing null. */
127  return (size_t) nprinted;
128  }
129 
130  /*
131  * We assume a C99-compliant vsnprintf, so believe its estimate of the
132  * required space, and add one for the trailing null. (If it's wrong, the
133  * logic will still work, but we may loop multiple times.)
134  *
135  * Choke if the required space would exceed MaxAllocSize. Note we use
136  * this palloc-oriented overflow limit even when in frontend.
137  */
138  if (unlikely((size_t) nprinted > MaxAllocSize - 1))
139  {
140 #ifndef FRONTEND
141  ereport(ERROR,
142  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
143  errmsg("out of memory")));
144 #else
145  fprintf(stderr, _("out of memory\n"));
146  exit(EXIT_FAILURE);
147 #endif
148  }
149 
150  return nprinted + 1;
151 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define fprintf
Definition: port.h:196
#define ERROR
Definition: elog.h:43
#define vsnprintf
Definition: port.h:191
static char * buf
Definition: pg_test_fsync.c:67
#define ereport(elevel, rest)
Definition: elog.h:141
#define MaxAllocSize
Definition: memutils.h:40
#define strerror
Definition: port.h:205
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define EXIT_FAILURE
Definition: settings.h:154
#define unlikely(x)
Definition: c.h:208
#define _(x)
Definition: elog.c:87