emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-C.el: Fix a number a bugs related to table parameters
@ 2020-11-06  4:55 Asa Zeren
  2020-12-12 18:15 ` Bastien
  0 siblings, 1 reply; 3+ messages in thread
From: Asa Zeren @ 2020-11-06  4:55 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 149 bytes --]

Here are a number of fixes to ob-C.el. There is still probably a bunch
of general cleanup to do in this file, but these changes make it work
for me.

[-- Attachment #2: 0001-ob-C.el-Fix-a-number-a-bugs-related-to-table-paramet.patch --]
[-- Type: application/octet-stream, Size: 4507 bytes --]

From 0cefbf3da37dea89058248b5cd9283db04b665aa Mon Sep 17 00:00:00 2001
From: Asa Zeren <asaizeren@gmail.com>
Date: Thu, 5 Nov 2020 23:23:10 -0500
Subject: [PATCH] ob-C.el: Fix a number a bugs related to table parameters

* ob-C.el (org-babel-C-expand-C, org-babel-C-header-to-C, org-babel-C-expand-D): Add a TYPE parameter to org-babel-C-header-to-C so that the helper functions return the correct type.
Previoulsy for tables of non-strings the helper functions would attempt to return a string, leading to compilation errors.

* ob-C.el (org-babel-C-header-to-C): Correctly quote header elements
Before, if the elisp value was not a string, it would not quote it, leading to UB when code attempted to access the header.

* ob-C.el (org-babel-C-utility-header-to-C): Include string.h in header
get_colum_num requires strcmp, leading to compilation errors.
---
 lisp/ob-C.el | 41 +++++++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)

diff --git a/lisp/ob-C.el b/lisp/ob-C.el
index c5155fbfc..874c8b217 100644
--- a/lisp/ob-C.el
+++ b/lisp/ob-C.el
@@ -257,7 +257,14 @@ its header arguments."
 		(when colnames
 		  (org-babel-C-utility-header-to-C))
 		;; tables headers
-		(mapconcat 'org-babel-C-header-to-C colnames "\n")
+		(mapconcat (lambda (head)
+                             (let* ((tblnm (car head))
+                                    (tbl (cdr (car (let* ((el vars))
+                                                (while (not (or (equal tblnm (caar el)) (not el)))
+                                                  (setq el (cdr el)))
+                                                el))))
+                                    (type (org-babel-C-val-to-base-type tbl)))
+                               (org-babel-C-header-to-C head type))) colnames "\n")
 		;; body
 		(if main-p
 		    (org-babel-C-ensure-main-wrap body)
@@ -289,7 +296,14 @@ its header arguments."
 		(when colnames
 		  (org-babel-C-utility-header-to-C))
 		;; tables headers
-		(mapconcat 'org-babel-C-header-to-C colnames "\n")
+		(mapconcat (lambda (head)
+                             (let* ((tblnm (car head))
+                                    (tbl (cdr (car (let* ((el vars))
+                                                (while (not (or (equal tblnm (caar el)) (not el)))
+                                                  (setq el (cdr el)))
+                                                el))))
+                                    (type (org-babel-C-val-to-base-type tbl)))
+                               (org-babel-C-header-to-C head type))) colnames "\n")
 		;; body
 		(if main-p
 		    (org-babel-C-ensure-main-wrap body)
@@ -425,7 +439,11 @@ of the same value."
 into a column number."
   (pcase org-babel-c-variant
     ((or `c `cpp)
-     "int get_column_num (int nbcols, const char** header, const char* column)
+     "
+#ifndef _STRING_H
+#include <string.h>
+#endif
+int get_column_num (int nbcols, const char** header, const char* column)
 {
   int c;
   for (c=0; c<nbcols; c++)
@@ -444,11 +462,18 @@ into a column number."
 }
 ")))
 
-(defun org-babel-C-header-to-C (head)
+(defun org-babel-C-header-to-C (head type)
   "Convert an elisp list of header table into a C or D vector
 specifying a variable with the name of the table."
+  (message "%S" type)
   (let ((table (car head))
-        (headers (cdr head)))
+        (headers (cdr head))
+        (typename (pcase type
+                    ('integerp "int")
+                    ('floatp "double")
+                    ('stringp (pcase org-babel-c-variant
+                                ((or 'c 'cpp) "const char*")
+                                ('d "string"))))))
     (concat
      (format
       (pcase org-babel-c-variant
@@ -456,13 +481,13 @@ specifying a variable with the name of the table."
 	(`d "string %s_header[%d] = [%s];"))
       table
       (length headers)
-      (mapconcat (lambda (h) (format "%S" h)) headers ","))
+      (mapconcat (lambda (h) (format "\"%s\"" h)) headers ","))
      "\n"
      (pcase org-babel-c-variant
        ((or `c `cpp)
 	(format
-	 "const char* %s_h (int row, const char* col) { return %s[row][get_column_num(%d,%s_header,col)]; }"
-	 table table (length headers) table))
+	 "%s %s_h (int row, const char* col) { return %s[row][get_column_num(%d,%s_header,col)]; }"
+	 typename table table (length headers) table))
        (`d
 	(format
 	 "string %s_h (size_t row, string col) { return %s[row][get_column_num(%s_header,col)]; }"
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] ob-C.el: Fix a number a bugs related to table parameters
  2020-11-06  4:55 [PATCH] ob-C.el: Fix a number a bugs related to table parameters Asa Zeren
@ 2020-12-12 18:15 ` Bastien
  2021-04-29 14:09   ` Bastien
  0 siblings, 1 reply; 3+ messages in thread
From: Bastien @ 2020-12-12 18:15 UTC (permalink / raw)
  To: Asa Zeren; +Cc: Thierry Banel, emacs-orgmode

Hi Asa,

Asa Zeren <asaizeren@gmail.com> writes:

> Here are a number of fixes to ob-C.el. There is still probably a bunch
> of general cleanup to do in this file, but these changes make it work
> for me.

thanks for the patch.  I'm copying Thierry as the (new) maintainer for
ob-C.el.

-- 
 Bastien


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ob-C.el: Fix a number a bugs related to table parameters
  2020-12-12 18:15 ` Bastien
@ 2021-04-29 14:09   ` Bastien
  0 siblings, 0 replies; 3+ messages in thread
From: Bastien @ 2021-04-29 14:09 UTC (permalink / raw)
  To: Asa Zeren; +Cc: Thierry Banel, emacs-orgmode

Hi Asa,

Bastien <bzg@gnu.org> writes:

> thanks for the patch.  I'm copying Thierry as the (new) maintainer for
> ob-C.el.

Thierry applied a variant of the patch in maint with commit 38f87a26.

Thanks,

-- 
 Bastien


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-04-29 14:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-06  4:55 [PATCH] ob-C.el: Fix a number a bugs related to table parameters Asa Zeren
2020-12-12 18:15 ` Bastien
2021-04-29 14:09   ` Bastien

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).