bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Extension api: get_argument return value and invalid flags combination


From: M
Subject: Extension api: get_argument return value and invalid flags combination
Date: Tue, 12 Dec 2023 00:36:34 +0100

From: crap0101
To: bug-gawk@gnu.org
Subject: Extension api: get_argument return value and invalid flags
combination

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -DNDEBUG
uname output: Linux orange 5.15.0-46-generic #49-Ubuntu SMP Thu Aug 4
18:03:25 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Gawk Version: 5.3.0

Attestation 1:
I have read https://www.gnu.org/software/gawk/manual/html_node/Bugs.html.
Yes

Attestation 2:
I have not modified the sources before building gawk.
True

Description:
Hi... here again :)
This time maybe with something more sensible.
Using the API function get_argument, as states in the doc `Return true if
the actual type matches wanted, and false otherwise. In the latter case,
result->val_type indicates the actual type` (mentioned also in section
17.4.9 and Table 17.2), I'm expecting that calling the function with, for
example, the $count argument of type bool and $wanted set to AWK_STRING
returns false, or when passing a string type as the $count argument and
requesting a AWK_UNDEFINED as $wanted, get_arguments returns false again.
But I noticed that if $wanted is AWK_UNDEFINED it returns always true.
Maybe is only a documentation bug?
btw, searching in the source code, in gawkapi.h, approx at line 707
(function node_to_awk_value), i see:

case AWK_UNDEFINED:
> /* return true and actual type for request of undefined */
>



Another thing... writing this test I got a warning about invalid flags
combination, which suggested to fill a bug report (so i'll can be partially
excused :D). Note that using the commented out printf doesn't show the
warning... could it have anything to do with the untyped/unassigned
behaviour with print we talked some weeks ago?
Below is an example code, attached the "valtype" test extension.


Repeat-By:
crap0101@orange:~/test$ cat r.awk

@load "valtype"


BEGIN {
    PROCINFO["sorted_in"] = "@ind_num_asc"
    arr[0] = "B"
    arr[1] = @/x/
    arr[2] = 2
    if ("mkbool" in FUNCTAB)
arr[3] = mkbool(0)
    arr[4]
    arr[5] = undef
    for (i in arr) {
printf("testing arr[%d] <%s> (%s)\n", i, arr[i], typeof(arr[i]))
#printf("testing arr[%d] (%s)\n", i, typeof(arr[i]))
valtype::test(arr[i])
    }
    printf("testing array 'arr'\n")
    valtype::test(arr)

    print "\n=============\n"
    valtype::test(arr[0], "AWK_ARRAY")
    valtype::test(arr[0], "AWK_NUMBER")
    valtype::test(arr[1], "AWK_NUMBER")
    valtype::test(arr[2], "AWK_STRING")
    valtype::test(arr[4], "AWK_STRING")
    valtype::test(arr[4], "AWK_REGEX")
}
crap0101@orange:~/test$ gawk5.3 -f r.awk
testing arr[0] <B> (string)
get_argument returns <1> (requested: AWK_UNDEFINED | got: AWK_STRING)
testing arr[1] <x> (regexp)
get_argument returns <1> (requested: AWK_UNDEFINED | got: AWK_REGEX)
testing arr[2] <2> (number)
get_argument returns <1> (requested: AWK_UNDEFINED | got: AWK_NUMBER)
testing arr[3] <0> (number|bool)
get_argument returns <1> (requested: AWK_UNDEFINED | got: AWK_BOOL)
testing arr[4] <> (untyped)
gawk: r.awk:17: attenzione: node_to_awk_value detected invalid flags
combination `MALLOC|STRING|STRCUR|NUMCUR|NUMBER'; please file a bug report
get_argument returns <0> (requested: AWK_UNDEFINED | got: AWK_UNDEFINED)
testing arr[5] <> (unassigned)
get_argument returns <1> (requested: AWK_UNDEFINED | got: AWK_UNDEFINED)
testing array 'arr'
get_argument returns <1> (requested: AWK_UNDEFINED | got: AWK_ARRAY)

=============

get_argument returns <0> (requested: AWK_ARRAY | got: AWK_UNDEFINED)
get_argument returns <1> (requested: AWK_NUMBER | got: AWK_NUMBER)
get_argument returns <0> (requested: AWK_NUMBER | got: AWK_REGEX)
get_argument returns <1> (requested: AWK_STRING | got: AWK_STRING)
get_argument returns <1> (requested: AWK_STRING | got: AWK_STRING)
gawk: r.awk:28: attenzione: node_to_awk_value detected invalid flags
combination `MALLOC|STRING|STRCUR|NUMCUR|NUMBER'; please file a bug report
get_argument returns <0> (requested: AWK_REGEX | got: AWK_UNDEFINED)
crap0101@orange:~/test$


crap0101@orange:~/test$ cat getarg_test.c

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "gawkapi.h"

#define __namespace__ "valtype"
#define DEBUG 0

int plugin_is_GPL_compatible;
static const gawk_api_t *api;
static awk_ext_id_t ext_id;
static const char *ext_version = "0.1";
static awk_value_t * do_test(int nargs, awk_value_t *result, struct
awk_ext_func *finfo);

static awk_ext_func_t func_table[] = {
  { "test", do_test, 2, 1, awk_false, NULL },
};

__attribute__((unused)) static awk_bool_t (*init_func)(void) = NULL;


int dl_load(const gawk_api_t *api_p, void *id) {
  api = api_p;
  ext_id = (awk_ext_id_t) &id;
  int errors = 0;
  long unsigned int i;

  if (api->major_version < 3) {
    fprintf(stderr, "incompatible api version:  %d.%d != %d.%d
(extension/gawk version)\n",
  GAWK_API_MAJOR_VERSION, GAWK_API_MINOR_VERSION, api->major_version,
api->minor_version);
    exit(1);
  }

  for (i=0; i < sizeof(func_table) / sizeof(awk_ext_func_t); i++) {
    if (! add_ext_func(__namespace__, & func_table[i])) {
      fprintf(stderr, "can't add extension function <%s>\n",
func_table[0].name);
      errors++;
    }
  }
  if (ext_version != NULL) {
    register_ext_version(ext_version);
  }
  return (errors == 0);
}


const char * VT[] = {
    "AWK_UNDEFINED",
    "AWK_NUMBER",
    "AWK_STRING",
    "AWK_REGEX",
    "AWK_STRNUM",
    "AWK_ARRAY",
    "AWK_SCALAR",
    "AWK_VALUE_COOKIE",
    "AWK_BOOL"
};

int get_valtype(char *s) {
  int i;
  for (i=0; i < 9; i++)
    if (0 == strcmp(s, VT[i]))
return i;
  return -1;
}

static awk_value_t*
do_test(int nargs,
 awk_value_t *result,
 __attribute__((unused)) struct awk_ext_func *finfo)
{
  /*
  * Test the $nargs[0] valtype.
  */

  assert(result != NULL);
  make_number(1.0, result);
  awk_value_t var_name, type_name;
  awk_valtype_t wanted;
  int ret;

  if (nargs == 2) {
    if (! get_argument(1, AWK_STRING, & type_name))
      fatal(ext_id, "2nd arg: wrong type");
    if (0 == type_name.str_value.len)
      fatal(ext_id, "2nd arg: empty");
    if (-1 == (ret = get_valtype(type_name.str_value.str)))
      fatal(ext_id, "2nd arg: unknown valtype <%s>",
type_name.str_value.str);
    wanted = (awk_valtype_t) ret;
  } else {
    wanted = AWK_UNDEFINED;
  }
  if (DEBUG)
    fprintf(stderr, "set wanted to %s (%d)\n", VT[wanted], wanted);

  ret = get_argument(0, wanted, & var_name);
  printf("get_argument returns <%d> (requested: %s | got: %s)\n",
ret, VT[wanted], VT[var_name.val_type]);
  return result;
}



Fix:
don't know yet.




thanks,
M.



-- 
me -> http://crap0101.altervista.org/

Attachment: getarg_test.c
Description: Text Data

Attachment: r.awk
Description: application/awk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]